Given a Binary Search Tree (BST), modify it so that all greater values in the given BST are added to every node.
Input Format:
The first line of input contains the number of test cases T. Each test case contains a string representing the tree as described below:
The values in the string are in the order of level order traversal of the tree where, numbers denote node values, and a character “N” denotes NULL child.
For example:
For the above tree, the string will be: 1 2 3 N N 4 6 N 5 N N 7 N
Output:
Print the inorder traversal of the modified BST.
Your Task:
In this function problem, the task is to complete the function modify which takes one argument: Address of the root of the BST. The function should contain the logic to modify the BST so that in the modified BST, every node has a value equal to the sum of its value in the original BST and values of all the elements larger than it in the original BST.
Expected Time Complexity: O(N)
Constraints:
1<=T<=10
1<=N<=100000
Example:
Input:
2
50 30 70 20 40 60 80
2 1 5 N N 4 7
Output:
350 330 300 260 210 150 80
19 18 16 12 7
Explanation:
50
/ \
30 70
/ \ / \
20 40 60 80
The above tree should be modified to following
260
/ \
330 150
/ \ / \
350 300 210 80
Note: The Input/Output format and Example is given are used for the system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from the stdin/console. The task is to complete the function specified, and not to write the full code.
If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.
saiujwal13083 | 347 |
isiddhisingh | 252 |
Abhishek_Jadhav | 243 |
_uncle_sam_ | 221 |
c_ocoooo | 217 |
saiujwal13083 | 610 |
SumitSingh27 | 466 |
c_ocoooo | 456 |
rathiarpit29 | 379 |
NaveenKumarNakka | 370 |
blackshadows | 5362 |
Ibrahim Nash | 5242 |
akhayrutdinov | 5111 |
mb1973 | 4931 |
Quandray | 4598 |