forked from yubinbai/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
48 lines (45 loc) · 1.42 KB
/
Solution.java
File metadata and controls
48 lines (45 loc) · 1.42 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
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
public class Solution {
private static int MINV = (int) -1e9;
public int maxPathSum(TreeNode root) {
int[] ret = _maxPathSum(root);
return ret[0];
}
/**
* get max path sum
* @param node
* @return ret, mutable array for return values
* ret[0] any path
* ret[1] path that ends at current node
*/
private int[] _maxPathSum(TreeNode node) {
if (node == null) return new int[] {MINV, MINV};
int[] ret = {node.val, node.val};
if (node.left == null && node.right == null) {
return ret;
}
int[] leftRet = _maxPathSum(node.left);
ret[0] = Math.max(ret[0], leftRet[0]);
ret[0] = Math.max(ret[0], node.val + leftRet[1]);
int[] rightRet = _maxPathSum(node.right);
ret[0] = Math.max(ret[0], rightRet[0]);
ret[0] = Math.max(ret[0], node.val + rightRet[1]);
ret[1] = Math.max(ret[1], node.val + Math.max(leftRet[1], rightRet[1]));
ret[0] = Math.max(ret[0], node.val + leftRet[1] + rightRet[1]);
return ret;
}
public static void main(String[] args) {
Solution s = new Solution();
TreeNode e = new TreeNode(-2);
e.left = new TreeNode(-1);
// e.right = new TreeNode(3);
System.out.println(s.maxPathSum(e));
}
}