17. BST to Greater Sum Tree
β GFG solution to the BST to Greater Sum Tree problem: transform each node to contain sum of all greater nodes using reverse inorder traversal. π
The problem can be found at the following link: π Question Link
π§© Problem Description
Given the root of a Binary Search Tree (BST) with unique node values, transform it into a greater sum tree where each node contains the sum of all nodes greater than that node.
A greater sum tree is a modified BST where every node's value is replaced by the sum of all node values that are strictly greater than the current node's value in the original BST.
π Examples
Example 1
Input: root = [11, 2, 29, 1, 7, 15, 40, N, N, N, N, N, N, 35, N]
Output: [119, 137, 75, 139, 130, 104, 0, N, N, N, N, N, N, 40, N]
Explanation: Every node is replaced with the sum of nodes greater than itself.
Example 2
Input: root = [2, 1, 6, N, N, 3, 7]
Output: [16, 18, 7, N, N, 13, 0]
Explanation: Every node is replaced with the sum of nodes greater than itself.
π Constraints
$1 \le \text{node->data} \le 3 \times 10^4$
$1 \le \text{number of nodes} \le 3 \times 10^4$
β
My Approach
The optimal approach uses Reverse Inorder Traversal to process nodes in descending order, maintaining a running sum of all processed nodes:
Reverse Inorder Traversal
Traversal Order:
Process right subtree first (larger values).
Then process current node.
Finally process left subtree (smaller values).
Maintain Running Sum:
Keep a cumulative sum variable that tracks total of all nodes processed so far.
Since we traverse from largest to smallest, this sum represents all greater values.
Update Node Values:
Store current node's original value temporarily.
Replace node's value with current cumulative sum (sum of all greater nodes).
Add original value to cumulative sum for next iterations.
Recursive Processing:
Base case: If node is null, return immediately.
Recursively process right subtree, update current node, then process left subtree.
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(n), where n is the number of nodes in the BST. We visit each node exactly once during the reverse inorder traversal.
Expected Auxiliary Space Complexity: O(h), where h is the height of the tree. This space is used by the recursion call stack. In the worst case (skewed tree), h = n, and in the best case (balanced tree), h = log(n).
π§βπ» Code (C++)
class Solution {
public:
void transformTree(Node* root) {
int s = 0;
function<void(Node*)> f = [&](Node* n) {
if (!n) return;
f(n->right);
int temp = n->data;
n->data = s;
s += temp;
f(n->left);
};
f(root);
}
};
β Code (Java)
class Solution {
int s = 0;
public void transformTree(Node root) {
if (root == null) return;
transformTree(root.right);
int temp = root.data;
root.data = s;
s += temp;
transformTree(root.left);
}
}
π Code (Python)
class Solution:
def transformTree(self, root):
self.s = 0
def f(n):
if not n: return
f(n.right)
temp = n.data
n.data = self.s
self.s += temp
f(n.left)
f(root)
π§ Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: π¬ Any Questions?. Let's make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated