-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTreeMaxSumPath.java
More file actions
38 lines (35 loc) · 880 Bytes
/
BinaryTreeMaxSumPath.java
File metadata and controls
38 lines (35 loc) · 880 Bytes
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
public class BinaryTreeMaxSumPath {
int curMax;
public int maxPathSum(TreeNode root) {
// Note: The Solution object is instantiated only once and is reused by each test case.
curMax = Integer.MIN_VALUE;
recur(root);
return curMax;
}
public int recur(TreeNode root) {
if (root == null) {
return 0;
}
int val = root.val;
int cur = val;
int left = recur(root.left);
int right = recur(root.right);
if (left > 0) {
cur += left;
}
if (right > 0) {
cur += right;
}
//System.out.println("val = " + val + "; curMax = " + curMax);
if (cur > curMax) {
curMax = cur;
}
System.out.println("return val = " + val);
return Math.max(val, Math.max(val + left, val + right));
}
public void test() {
TreeNode root = new TreeNode(-2);
root.left = new TreeNode(1);
System.out.println(maxPathSum(root));
}
}