-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathBT_Problem_20.java
More file actions
69 lines (57 loc) · 2.06 KB
/
Copy pathBT_Problem_20.java
File metadata and controls
69 lines (57 loc) · 2.06 KB
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
package trees.binaryTree;
/*
* Problem Title :- Construct Binary Tree from String with Bracket Representation.
*/
public class BT_Problem_20 {
/* A binary tree node has data, pointer to left
child and a pointer to right child */
static class Node {
int data;
BT_Problem_20.Node left, right;
public Node(int data) {
this.data = data;
left = right = null;
}
}
Node root;
// Convert a given tree to a tree where every node contains sum of
// values of nodes in left and right subtrees in the original tree
int toSumTree(Node node) {
// Base case
if (node == null)
return 0;
// Store the old value
int old_val = node.data;
// Recursively call for left and right subtrees and store the sum
// as new value of this node
node.data = toSumTree(node.left) + toSumTree(node.right);
// Return the sum of values of nodes in left and right subtrees
// and old_value of this node
return node.data + old_val;
}
// A utility function to print inorder traversal of a Binary Tree
void printInorder(Node node) {
if (node == null)
return;
printInorder(node.left);
System.out.print(node.data + " ");
printInorder(node.right);
}
/* Driver function to test above functions */
public static void main(String[] args) {
BT_Problem_20 tree = new BT_Problem_20();
/* Constructing tree given in the above figure */
tree.root = new Node(10);
tree.root.left = new Node(-2);
tree.root.right = new Node(6);
tree.root.left.left = new Node(8);
tree.root.left.right = new Node(-4);
tree.root.right.left = new Node(7);
tree.root.right.right = new Node(5);
tree.toSumTree(tree.root);
// Print inorder traversal of the converted tree to test result
// of toSumTree()
System.out.println("Inorder Traversal of the resultant tree is:");
tree.printInorder(tree.root);
}
}