-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaxSumPath.java
47 lines (33 loc) · 1.02 KB
/
maxSumPath.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
/*
Given a binary tree, find the maximum path sum.
The path may start and end at any node in the tree.
Example :
Given the below binary tree,
1
/ \
2 3
*/
long maxsum = Integer.MIN_VALUE;
public int maxPathSum(TreeNode a) {
if(a ==null)
return 0;
if(a.left==null && a.right ==null)
return a.val;
recur(a);
return (int)maxsum;
}
public int recur(TreeNode node){
if(node == null){
return Integer.MIN_VALUE;
}
if(node.left == null && node.right==null){
return node.val;
}
long leftval = recur(node.left);
long rightval = recur(node.right);
long maxchildval = Math.max(leftval, rightval);
long maxrootchildval = Math.max(node.val, Math.max(maxchildval, maxchildval + node.val));
long maxsubtree = Math.max(maxrootchildval, node.val + leftval + rightval);
maxsum = Math.max(maxsubtree, maxsum);
return (int)maxrootchildval;
}