-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathMaxPathSum.kt
72 lines (59 loc) · 1.9 KB
/
MaxPathSum.kt
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package com.daily.algothrim.leetcode.top150
import com.daily.algothrim.leetcode.TreeNode
import kotlin.math.max
/**
* 124. 二叉树中的最大路径和
*/
/*
二叉树中的 路径 被定义为一条节点序列,序列中每对相邻节点之间都存在一条边。同一个节点在一条路径序列中 至多出现一次 。该路径 至少包含一个 节点,且不一定经过根节点。
路径和 是路径中各节点值的总和。
给你一个二叉树的根节点 root ,返回其 最大路径和 。
示例 1:
输入:root = [1,2,3]
输出:6
解释:最优路径是 2 -> 1 -> 3 ,路径和为 2 + 1 + 3 = 6
示例 2:
输入:root = [-10,9,20,null,null,15,7]
输出:42
解释:最优路径是 15 -> 20 -> 7 ,路径和为 15 + 20 + 7 = 42
提示:
树中节点数目范围是 [1, 3 * 104]
-1000 <= Node.val <= 1000
*/
class MaxPathSum {
companion object {
@JvmStatic
fun main(args: Array<String>) {
println(MaxPathSum().maxPathSum(TreeNode(1).apply {
left = TreeNode(2)
right = TreeNode(3)
}))
println(MaxPathSum().maxPathSum(
TreeNode(-10).apply {
left = TreeNode(9)
right = TreeNode(20).apply {
left = TreeNode(15)
right = TreeNode(7)
}
}
))
}
}
private var maxLength = Int.MIN_VALUE
fun maxPathSum(root: TreeNode?): Int {
getMax(root)
return maxLength
}
/**
* O(n)
* O(n)
*/
private fun getMax(root: TreeNode?): Int {
if (root == null) return 0
val left = max(getMax(root.left), 0)
val right = max(getMax(root.right), 0)
val newLength = left + right + root.`val`
maxLength = max(maxLength, newLength)
return root.`val` + max(left, right)
}
}