FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
leetcode-study/binary-tree-maximum-path-sum/jdalma.kt at main · DaleStudy/leetcode-study · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
DaleStudy
/
leetcode-study
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
361
Star
155
Code
Issues
75
Pull requests
5
Discussions
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
leetcode-study
/
binary-tree-maximum-path-sum
/
jdalma.kt
Copy path
More file actions
More file actions
Latest commit
History
History
History
35 lines (27 loc) · 1 KB
Breadcrumbs
leetcode-study
/
binary-tree-maximum-path-sum
/
jdalma.kt
Copy path
File metadata and controls
35 lines (27 loc) · 1 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package
leetcode_study
import
io.kotest.matchers.shouldBe
import
org.junit.jupiter.api.Test
import
kotlin.math.max
class
`binary
-
tree
-
maximum
-
path
-
sum` {
/*
*
* TC: O(n), SC: O(log n)
*/
fun
maxPathSum
(
root
:
TreeNode
?
):
Int
{
if
(root
==
null
)
return
0
var
max
=
root.`
val
`
//
부모 노드와 2개의 자식 노드의 합을 전역 변수로 갱신한다.
fun
dfs
(
node
:
TreeNode
?
):
Int
{
if
(node
==
null
)
return
0
val
left
=
max(dfs(node.left),
0
)
val
right
=
max(dfs(node.right),
0
)
max
=
max(node.`
val
`
+
left
+
right, max)
return
node.`
val
`
+
max(left, right)
//
현재 노드와 2개의 자식 노드 중 최대의 값을 반환한다.
}
dfs(root)
return
max
}
@Test
fun
`이진 트리의 최대 경로 합을 반환한다`
() {
maxPathSum(
TreeNode
.of(
-
10
,
9
,
20
,
null
,
null
,
15
,
7
)) shouldBe
42
maxPathSum(
TreeNode
.of(
1
,
9
,
20
,
null
,
null
,
15
,
7
)) shouldBe
45
}
}
Back
|
FazBrowse Home
|
New Git URL