Convert BST to Greater Tree - Problem
Transform Your BST into a Greater Tree!

Given the root of a Binary Search Tree (BST), your task is to convert it into a Greater Tree where every node's value becomes the original value plus the sum of all values greater than it in the BST.

๐ŸŒณ What's a Greater Tree?
In a Greater Tree, each node stores the sum of itself and all nodes with larger values. This creates a fascinating transformation where larger values accumulate as we move towards smaller nodes!

๐Ÿ“ BST Properties Reminder:
โ€ข Left subtree contains only nodes with values less than the current node
โ€ข Right subtree contains only nodes with values greater than the current node
โ€ข Both subtrees are also valid BSTs

Example: If we have nodes [2, 5, 13], then:
โ€ข Node 13 becomes 13 (no greater values)
โ€ข Node 5 becomes 5 + 13 = 18
โ€ข Node 2 becomes 2 + 5 + 13 = 20

Input & Output

example_1.py โ€” Standard BST
$ Input: root = [4,1,6,0,2,5,7,null,null,null,3,null,null,null,8]
โ€บ Output: [30,36,21,36,35,26,15,null,null,null,33,null,null,null,8]
๐Ÿ’ก Note: Starting with BST values [0,1,2,3,4,5,6,7,8], each node gets sum of all greater values: Node 8โ†’8 (no greater), 7โ†’15 (7+8), 6โ†’21 (6+7+8), etc.
example_2.py โ€” Simple Three Node BST
$ Input: root = [2,null,5,null,null,null,13]
โ€บ Output: [20,null,18,null,null,null,13]
๐Ÿ’ก Note: Node 13 stays 13 (largest), Node 5 becomes 5+13=18, Node 2 becomes 2+5+13=20
example_3.py โ€” Single Node
$ Input: root = [5]
โ€บ Output: [5]
๐Ÿ’ก Note: A single node has no greater values, so it remains unchanged

Visualization

Tap to expand
BST โ†’ Greater Tree TransformationOriginal BST5213Greater Tree182013Transformation Process1. Traverse: 13 โ†’ 5 โ†’ 2 (reverse in-order)2. Running sum: 0+13=13, 13+5=18, 18+2=203. Update nodes: 13โ†’13, 5โ†’18, 2โ†’204. Result: Each node = original + sum of greater values
Understanding the Visualization
1
Original BST
Start with a valid BST where left < root < right
2
Identify traversal order
Reverse in-order gives us: 13 โ†’ 5 โ†’ 2 (descending)
3
Transform nodes
Running sum: 13 โ†’ 18 โ†’ 20, updating each node
4
Final Greater Tree
Each node now contains original + sum of greater values
Key Takeaway
๐ŸŽฏ Key Insight: Reverse in-order traversal of a BST visits nodes in descending order, allowing us to accumulate sums efficiently in a single pass!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(nยฒ)

For each of n nodes, we traverse all n nodes to find greater values

n
2n
โš  Quadratic Growth
Space Complexity
O(h)

Recursion stack depth equals tree height h

n
2n
โœ“ Linear Space

Constraints

  • The number of nodes in the tree is in the range [0, 104]
  • Node values are in range [-104, 104]
  • All node values are unique
  • The tree is guaranteed to be a valid binary search tree
Asked in
Amazon 15 Facebook 12 Google 8 Microsoft 6
125.0K Views
Medium Frequency
~15 min Avg. Time
2.9K 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