The "Binary Search Tree to Greater Sum Tree interview question" is a transformation problem. You are given the root of a Binary Search Tree (BST). Your goal is to modify the tree so that every node's value is replaced with the sum of all values in the original tree that were greater than or equal to that node's value. Because it's a BST, "greater values" are always located to the right of any given node.
Companies like Microsoft and Amazon ask the "Binary Search Tree to Greater Sum Tree coding problem" to test a candidate's understanding of Tree Traversal and the inherent properties of BSTs. It evaluates if you can manipulate a tree in-place and if you recognize that a specific type of traversal—Reverse In-order Traversal—visits nodes in descending order, which is perfect for maintaining a running sum.
This problem follows the Reverse In-order Traversal (Right-Root-Left) pattern.
running_sum (initially 0).running_sum, then update the node's value to the current running_sum.BST: 4 is root, 1 is left, 6 is right.
running_sum = . Node 6 becomes 6.running_sum = . Node 4 becomes 10.running_sum = . Node 1 becomes 11.
Resulting Tree Values: 10 (root), 11 (left), 6 (right).Always remember: In-order = Ascending, Reverse In-order = Descending for BSTs. If a problem depends on the relative rank of elements in a BST, one of these two traversals is almost certainly the key.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Convert BST to Greater Tree | Medium | Solve | |
| Recover Binary Search Tree | Medium | Solve | |
| Trim a Binary Search Tree | Medium | Solve | |
| Inorder Successor in BST | Medium | Solve | |
| Kth Smallest Element in a BST | Medium | Solve |