Watch 10 video solutions for Binary Search Tree to Greater Sum Tree, a medium level problem involving Tree, Depth-First Search, Binary Search Tree. This walkthrough by codestorywithMIK has 9,286 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Given the root of a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus the sum of all keys greater than the original key in BST.
As a reminder, a binary search tree is a tree that satisfies these constraints:
Example 1:
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]
Example 2:
Input: root = [0,null,1] Output: [1,null,1]
Constraints:
[1, 100].0 <= Node.val <= 100
Note: This question is the same as 538: https://leetcode.com/problems/convert-bst-to-greater-tree/
Problem Overview: You are given a binary search tree where each node must be updated so its new value equals the original value plus the sum of all keys greater than it. The structure of the tree stays the same; only node values change.
Approach 1: Reverse In-Order Traversal (DFS) (Time: O(n), Space: O(h))
A BST visited in reverse in-order order (right → node → left) processes nodes from largest to smallest. Maintain a running variable sum that stores the cumulative total of nodes already visited. When you reach a node, add its value to sum and update the node with this new total. Because nodes are visited in descending order, sum always represents the sum of all greater values. This approach relies on depth-first search and the ordering property of a BST. The recursion depth equals the tree height h, giving O(h) auxiliary space. For balanced trees this is O(log n), while a skewed tree degrades to O(n).
The key insight is exploiting the BST ordering. Instead of computing greater sums by scanning other nodes, you accumulate them naturally while traversing from the largest value downward. This produces an optimal linear-time solution and minimal extra state.
Approach 2: Iterative Traversal Using a Stack (Time: O(n), Space: O(h))
The same reverse in-order logic can be implemented iteratively with an explicit stack. Start from the root and push nodes while moving right to reach the maximum element. Pop a node, update the running sum, modify the node value, then move to its left subtree. Repeat the process until the stack becomes empty. Each node is pushed and popped once, keeping the time complexity O(n).
This approach avoids recursion and is useful in environments where stack depth is a concern. The stack stores at most the height of the tree, so space complexity remains O(h). The algorithm still leverages the sorted property of a binary tree structured as a BST, ensuring nodes are processed in descending order.
Recommended for interviews: Reverse in-order traversal is the expected solution. It demonstrates understanding of BST ordering and how DFS traversal patterns can transform data efficiently. Mentioning the iterative stack variant shows awareness of recursion limits and gives a practical alternative while keeping the same O(n) performance.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Reverse In-Order Traversal (DFS) | O(n) | O(h) | Preferred solution for BST problems; simple and optimal when recursion depth is manageable. |
| Iterative Traversal Using Stack | O(n) | O(h) | Useful when avoiding recursion or when stack overflow risk exists for deep trees. |