Given a Binary Search Tree (BST), modify it so that all greater values in the given BST are added to every node. For example, consider the following BST.
50 / \ 30 70 / \ / \ 20 40 60 80 The above tree should be modified to following 260 / \ 330 150 / \ / \ 350 300 210 80
A simple method for solving this is to find the sum of all greater values for every node. This method would take O(n^2) time.
The method discussed in this article uses the technique of reverse in-order tree traversal of BST which optimises the problem to be solved in a single traversal.
Approach: In this problem as we could notice that the largest node would remain the same. The value of 2nd largest node = value of largest + value of second largest node. Similarly, the value of nth largest node will be the sum of the n-th node and value of (n-1)th largest node after modification. So if we traverse the tree in descending order and simultaneously update the sum value at every step while adding the value to the root node, the problem would be solved.
So to traverse the BST in descending order we use reverse in-order traversal of BST. This takes a global variable sum which is updated at every node and once the root node is reached it is added to the value of root node and value of the root node is updated.
350 330 300 260 210 150 80
- Time Complexity: O(n).
As this problem uses an in-order tree traversal technique
- Auxiliary Space: O(1).
As no data structure has been used for storing values.
As a side note, we can also use reverse Inorder traversal to find the kth largest element in a BST.
This article is contributed by Chandra Prakash. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Convert a BST to a Binary Tree such that sum of all greater keys is added to every key
- Implementing a BST where every node stores the maximum number of nodes in the path till any leaf
- K'th Largest Element in BST when modification to BST is not allowed
- Convert a normal BST to Balanced BST
- Two nodes of a BST are swapped, correct the BST | Set-2
- Two nodes of a BST are swapped, correct the BST
- Find k-th smallest element in BST (Order Statistics in BST)
- Queries to find sum of distance of a given node to every leaf node in a Weighted Tree
- Transform a BST to greater sum tree
- Smallest number in BST which is greater than or equal to N
- Smallest number in BST which is greater than or equal to N ( Iterative Approach)
- Count pairs in BST with sum greater than K
- Check if a Binary Tree (not BST) has duplicate values
- Sum of cousin nodes of a given node in a BST
- Total sum except adjacent of a given node in BST
- Find Leftmost and Rightmost node of BST from its given preorder traversal
- Print all the nodes except the leftmost node in every level of the given binary tree
- Find all the pairs with given sum in a BST | Set 2
- Check if each internal node of a BST has exactly one child
- Replace every element with the least greater element on its right