-
Notifications
You must be signed in to change notification settings - Fork 506
/
Copy pathTransform to Sum Tree.cpp
60 lines (47 loc) · 1.39 KB
/
Transform to Sum Tree.cpp
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
/*
Transform to Sum Tree
=====================
Given a Binary Tree of size N , where each node can have positive or negative values. Convert this to a tree where each node contains the sum of the left and right sub trees of the original tree. The values of leaf nodes are changed to 0.
Example 1:
Input:
10
/ \
-2 6
/ \ / \
8 -4 7 5
Output:
20
/ \
4 12
/ \ / \
0 0 0 0
Explanation:
(4-2+12+6)
/ \
(8-4) (7+5)
/ \ / \
0 0 0 0
Your Task:
You dont need to read input or print anything. Complete the function toSumTree() which takes root node as input parameter and modifies the given tree in-place.
Note: If you click on Compile and Test the output will be the in-order traversal of the modified tree.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(height of tree)
Constraints:
1 ≤ N ≤ 104
*/
int helper(Node *node)
{
if (!node)
return 0;
int left = helper(node->left);
int right = helper(node->right);
int ans = left + right + node->data;
node->data = left + right;
return ans;
}
// 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
void toSumTree(Node *node)
{
helper(node);
}