-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbinaryTreeMaximumPathSum.java
61 lines (55 loc) · 1.51 KB
/
binaryTreeMaximumPathSum.java
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
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
/*
要注意分清返回值和最大值,不能把计算出来的最大值返回回去,否则会有问题。
1
1 -3
3
计算到-3节点时,它求出的maxSum(root.left)是3,可以用3更新max,但不能把3返回给1,返回给1的话,会算出最大值为4。
-3节点实际返回给1的是0。
*/
public class Solution {
int max = Integer.MIN_VALUE;
public int maxSum(TreeNode root) {
if(root==null) {
return 0;
}
int value = root.val;
int lmax = 0;
int rmax = 0;
if(root.left!=null) {
lmax = maxSum(root.left);
if(lmax>0) {
value += lmax;
}
}
if(root.right!=null) {
rmax = maxSum(root.right);
if(rmax>0) {
value += rmax;
}
}
//更新最大值
//max is the max of {root.val,root.val+lmax,root.val+rmax, root.val + lmax + rmax}
if (value>max) {
max = value;
}
//返回值
//return max of (root.val, root.val + lmax, root.val + rmax)
return Math.max(root.val,Math.max(root.val + lmax, root.val + rmax));
}
public int maxPathSum(TreeNode root) {
if(root==null) {
return 0;
}
maxSum(root);
return max;
}
}