-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMaximumPathSumBetweenNodes.java
66 lines (51 loc) · 1.44 KB
/
MaximumPathSumBetweenNodes.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
62
63
64
65
66
public class MaximumPathSumBetweenNodes{
class Res{
int val;
}
public int maxPathSumBetweenNodesUtil(TreeNode root, Res res){
if(root == null){
return 0;
}
int l = maxPathSumBetweenNodesUtil(root.left, res);
int r = maxPathSumBetweenNodesUtil(root.right, res);
int max_left_or_right = Math.max(Math.max(l,r)+root.val, root.val);
int max_including_root = Math.max(max_left_or_right, l+r+root.val);
res.val = Math.max(res.val, max_including_root);
return max_left_or_right;
}
public int maxPathSumBetweenNodes(TreeNode root){
Res res = new Res();
res.val = Integer.MIN_VALUE;
maxPathSumBetweenNodesUtil(root, res);
return res.val;
}
class TreeNode{
TreeNode left, right;
int val;
TreeNode(int val){
this.val = val;
this.left = null;
this.right = null;
}
}
public void inOrder(TreeNode root){
if(root != null){
inOrder(root.left);
System.out.print(root.val+"->");
inOrder(root.right);
}
}
public TreeNode addNode(int val){
return new TreeNode(val);
}
public static void main(String [] args){
MaximumPathSumBetweenNodes test = new MaximumPathSumBetweenNodes();
MaximumPathSumBetweenNodes.TreeNode root = test.addNode(-5);
root.left = test.addNode(2);
root.right = test.addNode(10);
root.left.left = test.addNode(2);
root.left.right = test.addNode(3);
root.left.right.left = test.addNode(-2);
System.out.println(test.maxPathSumBetweenNodes(root));
}
}