forked from ppsirker/dsalgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryTreeSumChildNodes.java
105 lines (95 loc) · 1.82 KB
/
BinaryTreeSumChildNodes.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
For problem and solution description please visit the link below
http://www.dsalgo.com/2013/02/BinaryTreeSumChildNodes.php.html
*/
package com.dsalgo;
/*
* In a binary tree change each node's value(except leaf node) as the sum of left and right subtree's value.
*
*
Constructed binary tree is:
1
/ \
2 3
/ \ \
4 5 8
/ \
6 7
After sum stored is in each node, binary tree is:
35
/ \
9 21
/ \ \
4 5 13
/ \
6 7
*/
public class BinaryTreeSumChildNodes
{
/**
* @param args
*/
public static void main(String[] args)
{
Node a = new Node(1);
Node b = new Node(2);
Node c = new Node(3);
Node d = new Node(4);
Node e = new Node(5);
Node f = new Node(8);
Node g = new Node(6);
Node h = new Node(7);
a.left = b;
a.right = c;
b.left = d;
b.right = e;
c.right = f;
f.left = g;
f.right = h;
printNice(a);
System.out.println();
makeSum(a);
printNice(a);
}
public static int makeSum(Node root)
{
if (root == null)
return 0;
int temp = root.value;
int sum = makeSum(root.left) + makeSum(root.right);
if (root.left != null || root.right != null)
root.value = sum;
return temp + sum;
}
public static void printNice(Node root)
{
if (root == null)
return;
else
{
System.out.print(root.value);
if (root.left != null)
{
System.out.print("L->[");
printNice(root.left);
System.out.print("]");
}
if (root.right != null)
{
System.out.print("R->[");
printNice(root.right);
System.out.print("]");
}
}
}
static class Node
{
public Node left;
public Node right;
public int value;
public Node(int value)
{
this.value=value;
}
}
}