-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChildrenSumProperty
50 lines (39 loc) · 1.26 KB
/
ChildrenSumProperty
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
/*************************************************************
Following is the Binary Tree node structure
class BinaryTreeNode < Integer > {
int data;
BinaryTreeNode < Integer > left;
BinaryTreeNode < Integer > right;
public BinaryTreeNode(int data) {
this.data = data;
}
}
*************************************************************/
public class Solution {
public static void changeTree(BinaryTreeNode < Integer > root) {
if(root==null)
return;
int child = 0;
if(root.left!=null)
child += root.left.data;
if(root.right!=null)
child += root.right.data;
if(child>=root.data)
root.data = child;
else{
if(root.left!=null)
root.left.data = root.data;
else if(root.right!=null)
root.right.data = root.data;
}
changeTree(root.left);
changeTree(root.right);
int tot = 0;
if(root.left!=null)
tot += root.left.data;
if(root.right!=null)
tot += root.right.data;
if(root.left!=null || root.right!=null)
root.data = tot;
}
}