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
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
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
โ Quadratic Growth
Space Complexity
O(h)
Recursion stack depth equals tree height h
โ 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
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code