plz check the code
Replace with Sum of greater nodes
Hello @akb.tech17
In this question you have to create a balanced binary search tree first from the input array and then replace each node with the sum of all nodes greater than this node.
Ex:-
7
20 30 40 50 60 70 80
Then balanced binary search would be
50
/ \
30 70
/ \ / \
20 40 60 80
When the nodes are replaced with the sum of the nodes greater than that particular node, Tree becomes
260
/ \
330 150
/ \ / \
350 300 210 80
And it’s preorder traversal is 260 330 350 300 150 210 80
which is the answer.
To create a balanced binary search Tree from a sorted array, do the following
- Get the Middle of the array and make it root.
- Recursively do same for left half and right half.
a) Get the middle of left half and make it left child of the root
created in step 1.
b) Get the middle of right half and make it right child of the
root created in step 1.