-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaximumSumTree.java
More file actions
36 lines (34 loc) · 905 Bytes
/
MaximumSumTree.java
File metadata and controls
36 lines (34 loc) · 905 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
package Tree;
public class MaximumSumTree {
static class Node
{
int key;
Node left, right;
}
static int ans = Integer.MIN_VALUE;
static Node newNode(int key)
{
Node temp = new Node();
temp.key = key;
temp.left = temp.right = null;
return temp;
}
static int maxSum(Node root) {
if(root == null) return 0;
int l = maxSum(root.left);
int r = maxSum(root.right);
int sum = root.key + l+r;
ans = Math.max(ans, sum);
return sum;
}
public static void main(String args[]) {
Node root = newNode(1);
root.left = newNode(-2);
root.right = newNode(3);
root.left.left = newNode(4);
root.left.right = newNode(5);
root.right.left = newNode(-6);
root.right.right = newNode(2);
System.out.println(maxSum(root));
}
}