forked from DengWangBao/Leetcode-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTreeMaximumPathSum.java
More file actions
36 lines (32 loc) · 1.04 KB
/
BinaryTreeMaximumPathSum.java
File metadata and controls
36 lines (32 loc) · 1.04 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
/**
* 单边的或者双边的,或只包含根节点的
*/
public class BinaryTreeMaximumPathSum {
public int maxPathSum(TreeNode root) {
return maxPathSum(root, null);
}
/**
* max表示包含root的单边路径最大和
*/
private int maxPathSum(TreeNode root, int[] max) {
if (root == null) {
return Integer.MIN_VALUE; // 此处容易错
}
int[] left = new int[1], right = new int[1];
int leftMax = maxPathSum(root.left, left);
int rightMax = maxPathSum(root.right, right);
if (max != null) {
max[0] = max(left[0], right[0], 0) + root.val; // 此处容易错
}
// 容易错,要考虑到所有可能的情况
return max(leftMax, rightMax, root.val, left[0] + right[0] + root.val,
left[0] + root.val, right[0] + root.val);
}
private int max(int... vals) {
int max = Integer.MIN_VALUE;
for (int val : vals) {
max = Math.max(max, val);
}
return max;
}
}