Geeksforgeeks

Error

×

Leaderboard

Showing:

Handle | Score |
---|---|

@Ibrahim Nash | 6420 |

@blackshadows | 6376 |

@mb1973 | 5604 |

@Quandray | 5231 |

@akhayrutdinov | 5111 |

@saiujwal13083 | 4510 |

@sanjay05 | 3762 |

@kirtidee18 | 3673 |

@marius_valentin_dragoi | 3522 |

@sushant_a | 3459 |

@verma_ji | 3413 |

Complete Leaderboard | |

Handle | Score |

@codersgroup18 | 799 |

@sriramgoparaju99 | 737 |

@Manikanta punnam | 629 |

@rdakka | 606 |

@prankursharma31 | 597 |

@praveenbgp6 | 546 |

@sanskar94511 | 531 |

@purohitmn02 | 529 |

@yashkaril4 | 517 |

@abhikarshgupta | 474 |

@rohitgarg2825 | 464 |

Complete Leaderboard |

Given a BST, modify it so that all greater values in the given BST are added to every node.

**Example 1:**

**Input:**
50
/ \
30 70
/ \ / \
20 40 60 80**
Output: **350 330 300 260 210 150 80**
Explanation:**The tree should be modified to
following:
260
/ \
330 150
/ \ / \
350 300 210 80

**Example 2:**

**Input:**
2
/ \
1 5
/ \
4 7**
Output: **19 18 16 12 7

**Your Task:**

You don't need to read input or print anything. Your task is to complete the function **modify() **which takes one argument: 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. Return the root of the modified BST. The driver code will print the inorder traversal of the returned BST/

**Expected Time Complexity: **O(N)

**Expected Auxiliary Space: **O(Height of the BST).

**Constraints:**

1<=N<=100000

**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.

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes
Add all greater values to every node in a BST

...