Error
|
|
---|---|
@Ibrahim Nash | 5761 |
@blackshadows | 5715 |
@akhayrutdinov | 5111 |
@mb1973 | 4989 |
@Quandray | 4944 |
@saiujwal13083 | 4506 |
@sanjay05 | 3762 |
@marius_valentin_dragoi | 3516 |
@sushant_a | 3459 |
@verma_ji | 3341 |
@KshamaGupta | 3318 |
Complete Leaderboard | |
|
|
@aroranayan999 | 1115 |
@bt8816103042 | 739 |
@SherlockHolmes3 | 447 |
@codeantik | 441 |
@SHOAIBVIJAPURE | 430 |
@shalinibhataniya1097 | 408 |
@ShamaKhan1 | 392 |
@neverevergiveup | 381 |
@amrutakashikar2 | 355 |
@mahlawatep | 353 |
@murarry3625 | 352 |
Complete Leaderboard |
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
We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?
Yes