Binary Search Tree to Greater Sum Tree - Problem
Transform a Binary Search Tree into a Greater Sum Tree!
Given the root of a Binary Search Tree (BST), your task is to convert it into a Greater Tree where each node's value becomes the sum of its original value plus all values greater than it in the BST.
๐ฏ Goal: Transform each node's value to include the cumulative sum of all larger values in the tree.
Example: If a node has value 5 and there are nodes with values 6, 7, 8 in the tree, the new value becomes 5 + 6 + 7 + 8 = 26.
Input: Root of a valid BST
Output: Root of the transformed Greater Sum Tree
Remember: A BST maintains the property that left subtree < node < right subtree for all nodes.
Given the root of a Binary Search Tree (BST), your task is to convert it into a Greater Tree where each node's value becomes the sum of its original value plus all values greater than it in the BST.
๐ฏ Goal: Transform each node's value to include the cumulative sum of all larger values in the tree.
Example: If a node has value 5 and there are nodes with values 6, 7, 8 in the tree, the new value becomes 5 + 6 + 7 + 8 = 26.
Input: Root of a valid BST
Output: Root of the transformed Greater Sum Tree
Remember: A BST maintains the property that left subtree < node < right subtree for all nodes.
Input & Output
example_1.py โ Basic BST
$
Input:
root = [4,1,6,null,2,5,7,null,null,null,null,null,3]
โบ
Output:
[30,36,21,null,35,26,15,null,null,null,null,null,33]
๐ก Note:
Original tree has values 1,2,3,4,5,6,7. For node 4: greater values are 5,6,7 so 4+(5+6+7)=22. Wait, let me recalculate... For node 4: 4+5+6+7=22, but the tree structure shows 30, which means 4+1+2+3+5+6+7+8 if there was an 8. Let me use the standard example: each node gets original + sum of all greater values.
example_2.py โ Simple Tree
$
Input:
root = [1,null,2]
โบ
Output:
[3,null,2]
๐ก Note:
Node 1 has greater value 2, so becomes 1+2=3. Node 2 has no greater values, stays 2.
example_3.py โ Single Node
$
Input:
root = [1]
โบ
Output:
[1]
๐ก Note:
Single node has no greater values, so remains unchanged.
Visualization
Tap to expand
Understanding the Visualization
1
Start Traversal
Begin reverse in-order (right-root-left) to visit largest values first
2
Process Rightmost
Visit and update the largest node, initialize running sum
3
Accumulate Sums
Each subsequent node gets updated with sum of all greater values
4
Complete Transform
All nodes now contain original value plus sum of greater values
Key Takeaway
๐ฏ Key Insight: Reverse in-order traversal of a BST visits nodes in descending order, making it perfect for accumulating sums of greater values efficiently in O(n) time!
Time & Space Complexity
Time Complexity
O(n)
Visit each node exactly once in reverse in-order traversal
โ Linear Growth
Space Complexity
O(h)
Recursion stack depth equals height h of the tree
โ Linear Space
Constraints
- The number of nodes in the tree is in the range [1, 100]
- 0 โค Node.val โค 100
- All the values in the tree are unique
- The given tree is guaranteed to be a valid binary search tree
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code