Skip to content
← All posts

Convert BST to Greater Tree: Reverse Inorder Traversal

4 min read
leetcodeproblemmediumtrees

You are given the root of a binary search tree. Convert it to a Greater Tree where every node's new value equals its original value plus the sum of all values greater than it in the original BST.

This is LeetCode 538: Convert BST to Greater Tree, a medium problem that tests whether you can think about BST traversal order in a creative way. The trick is realizing that one specific traversal order gives you exactly what you need.

The problem

Given a BST, replace each node's value with the sum of all values that are greater than or equal to that node's original value. The tree structure stays the same, only the values change.

For example, in a BST with values [0, 1, 2, 4, 5, 6, 7], the largest node (7) stays 7 because nothing is greater. Node 6 becomes 6 + 7 = 13. Node 4 becomes 4 + 5 + 6 + 7 = 22. The smallest node (0) becomes the total sum of the entire tree.

original BST4160257greater tree22was 425was 113was 625was 024was 218was 57was 7
Each node's new value = its original value + sum of all values greater than it in the BST. Node 7 stays 7 (nothing greater). Node 6 becomes 6 + 7 = 13. Node 4 becomes 4 + 5 + 6 + 7 = 22.

The key insight

In a BST, an inorder traversal (left, node, right) visits nodes in ascending order. If you reverse that, doing right, node, left, you visit nodes from largest to smallest. That is exactly the order you need.

Keep a running sum as you traverse. At each node, add the node's value to the running sum, then replace the node's value with the running sum. Because you are visiting from largest to smallest, the running sum at any point is the sum of all values greater than or equal to the current node.

Reverse inorder traversal (right, node, left) visits BST nodes from largest to smallest. A single running sum variable is all you need to accumulate the greater-than totals as you go.

The solution

def convert_bst(root):
    total = 0

    def traverse(node):
        nonlocal total
        if node is None:
            return
        traverse(node.right)
        total += node.val
        node.val = total
        traverse(node.left)

    traverse(root)
    return root

Step-by-step walkthrough

Let's trace through the tree [4, 1, 6, 0, 2, 5, 7] using reverse inorder traversal. Watch how the running sum grows as we visit nodes from largest to smallest.

Step 1: Visit node 7. Running sum = 0 + 7 = 7. Update node to 7.

4160257sum=7

Start at the rightmost node (largest value). It has no greater values, so it keeps its value of 7.

Step 2: Visit node 6. Running sum = 7 + 6 = 13. Update node to 13.

4113sum=130257

Move to node 6 (parent of 7). Add running sum to get 13: the sum of all values >= 6.

Step 3: Visit node 5. Running sum = 13 + 5 = 18. Update node to 18.

41130218sum=187

Traverse left from node 6 to node 5. The running sum 18 = 5 + 6 + 7.

Step 4: Visit node 4 (root). Running sum = 18 + 4 = 22. Update node to 22.

22sum=2211302187

Back at the root. The running sum 22 = 4 + 5 + 6 + 7, the sum of all values >= 4.

Step 5: Visit node 2. Running sum = 22 + 2 = 24. Update node to 24.

22113024sum=24187

Now into the left subtree. Node 2 gets 24 = 2 + 4 + 5 + 6 + 7.

Step 6: Visit node 1. Running sum = 24 + 1 = 25. Update node to 25.

2225sum=2513024187

Node 1 gets 25 = 1 + 2 + 4 + 5 + 6 + 7. Only node 0 is smaller.

Step 7: Visit node 0. Running sum = 25 + 0 = 25. Update node to 25.

22251325sum=2524187

The leftmost node (smallest value). Its new value is the total sum of all nodes: 0 + 1 + 2 + 4 + 5 + 6 + 7 = 25.

Done: All nodes updated. The BST is now a Greater Tree.

22done25132524187

Every node's value is now the sum of all original values >= its original value. The tree structure is unchanged.

Notice the pattern: each node's new value is simply the running sum after adding its original value. Because we visit from right to left, the running sum always contains exactly the values that are greater than or equal to the current node.

Complexity analysis

ApproachTimeSpace
Reverse inorder traversalO(n)O(h) recursion stack

Time: O(n) because we visit every node exactly once and do O(1) work at each node (one addition and one assignment).

Space: O(h) where h is the height of the tree, due to the recursion stack. For a balanced BST, h = log n. For a completely skewed tree, h = n.

The building blocks

1. Reverse inorder traversal

Standard inorder (left, node, right) gives you sorted order in a BST. Reversing it to (right, node, left) gives you reverse sorted order. This is useful any time you need to process BST nodes from largest to smallest. The recursive structure is identical to regular inorder, you just swap which child you visit first.

2. Running sum accumulation

Instead of computing the sum of all greater values from scratch for each node (which would be O(n) per node, O(n^2) total), you carry a single running sum through the traversal. Each node adds its value to the sum and then uses that sum as its new value. This reduces the work to O(1) per node.

Edge cases

  • Single node tree: the node's value stays the same, since no other values exist in the tree and the sum of values >= itself is just itself
  • Left-skewed tree (descending chain): reverse inorder visits from root down, the running sum grows with each step
  • Right-skewed tree (ascending chain): reverse inorder visits from the bottom up, the leaf keeps its value and each parent accumulates more
  • Tree with negative values: the algorithm works the same way, the running sum can decrease when adding negative values

From understanding to recall

You just traced through a reverse inorder traversal and saw how a single running sum variable converts an entire BST into a Greater Tree. The core idea, visiting nodes from largest to smallest and accumulating as you go, is elegant and efficient.

But recognizing this pattern under interview pressure is harder than understanding it now. The connection between "sum of all greater values" and "reverse inorder" is not obvious until you have drilled it a few times. Spaced repetition helps you internalize the link between the problem shape and the traversal order, so that when you see a similar problem, the approach comes to mind automatically.

Related posts