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.

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
BST โ†’ Greater Sum Tree TransformationOriginal BST4261357TransformGreater Sum Tree2220132221127Reverse In-Order Traversal Process:Step 1: Visit rightmost node (7) โ†’ running_sum = 7, update node to 7Step 2: Visit node (6) โ†’ running_sum = 7+6 = 13, update node to 13Step 3: Visit node (5) โ†’ running_sum = 13+5 = 18, update node to 12 (5+7)Step 4: Visit root (4) โ†’ running_sum = 18+4 = 22, update node to 22Step 5: Visit node (3) โ†’ running_sum = 22+3 = 25, update to 21 (3+4+5+6+7)Step 6: Visit node (2) โ†’ running_sum = 25+2 = 27, update to 20 (2+3+4+5+6+7)Step 7: Visit leftmost (1) โ†’ running_sum = 27+1 = 28, update to 22 (1+2+3+4+5+6+7)๐ŸŽฏ Key Insight:Reverse in-order visits nodes largest-to-smallest,perfect for accumulating greater values!
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

n
2n
โœ“ Linear Growth
Space Complexity
O(h)

Recursion stack depth equals height h of the tree

n
2n
โœ“ 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
Asked in
Amazon 45 Google 38 Microsoft 32 Meta 28
89.3K Views
Medium Frequency
~15 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen