-
Notifications
You must be signed in to change notification settings - Fork 125
Expand file tree
/
Copy pathL0124_BinaryTreeMaximumPathSum.java
More file actions
88 lines (75 loc) · 2.88 KB
/
L0124_BinaryTreeMaximumPathSum.java
File metadata and controls
88 lines (75 loc) · 2.88 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import common.TreeNode;
/**
* 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 * 10^4]
* -1000 <= Node.val <= 1000
*/
public class L0124_BinaryTreeMaximumPathSum {
private int maxSum;
public int maxPathSum(TreeNode root) {
maxSum = Integer.MIN_VALUE;
maxGain(root);
return maxSum;
}
/**
* 计算从某个节点出发的最大路径和
*
* @param node 当前节点
* @return 从当前节点出发的最大路径和
*/
private int maxGain(TreeNode node) {
if (node == null) {
return 0;
}
// 递归计算左右子节点的最大贡献值
// 只有在最大贡献值大于 0 时,才会选取对应子节点
int leftGain = Math.max(maxGain(node.left), 0);
int rightGain = Math.max(maxGain(node.right), 0);
// 节点的最大路径和取决于该节点的值与该节点的左右子节点的最大贡献值
int priceNewPath = node.val + leftGain + rightGain;
// 更新答案
maxSum = Math.max(maxSum, priceNewPath);
// 返回节点的最大贡献值
return node.val + Math.max(leftGain, rightGain);
}
public static void main(String[] args) {
L0124_BinaryTreeMaximumPathSum solution = new L0124_BinaryTreeMaximumPathSum();
// 测试用例 1
TreeNode root1 = new TreeNode(1);
root1.left = new TreeNode(2);
root1.right = new TreeNode(3);
System.out.println(solution.maxPathSum(root1)); // 应输出 6
// 测试用例 2
TreeNode root2 = new TreeNode(-10);
root2.left = new TreeNode(9);
root2.right = new TreeNode(20);
root2.right.left = new TreeNode(15);
root2.right.right = new TreeNode(7);
System.out.println(solution.maxPathSum(root2)); // 应输出 42
// 测试用例 3:单节点
TreeNode root3 = new TreeNode(1);
System.out.println(solution.maxPathSum(root3)); // 应输出 1
// 测试用例 4:全负数
TreeNode root4 = new TreeNode(-3);
root4.left = new TreeNode(-2);
root4.right = new TreeNode(-1);
System.out.println(solution.maxPathSum(root4)); // 应输出 -1
}
}