-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaximumTreeSum1.java
50 lines (41 loc) · 1.21 KB
/
MaximumTreeSum1.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
/**
* Created by Yuzhang on 1/30/17.
* from any Leaf Node to Leaf Node
*/
public class MaximumTreeSum1 {
static class TreeNode {
int key;
TreeNode left, right;
public TreeNode(int key) {
this.key = key;
left = right = null;
}
}
public int maxPathSum(TreeNode root) {
int[] max = new int[]{Integer.MIN_VALUE};
helper(root, max);
return max[0];
}
private int helper(TreeNode root, int[] max) {
if (root == null) {
return 0;
}
int left = helper(root.left, max);
int right = helper(root.right, max);
if (root.left != null && root.right != null) {
max[0] = Math.max(max[0], root.key + left + right);
return Math.max(left, right) + root.key;
}
return root.left == null ? right + root.key : left + root.key;
}
TreeNode root;
public static void main(String args[])
{
MaximumTreeSum1 tree = new MaximumTreeSum1();
tree.root = new TreeNode(4);
tree.root.left = null;
tree.root.right = new TreeNode(5);
int result = tree.maxPathSum(tree.root);
System.out.println(result);
}
}