Replace Each BST Score With Its Running Sum From the Top
Last updated: November 29, 2026
Quick Overview
Given a BST of ranking scores, replace each node's value with the sum of all values in the tree greater than or equal to it, in place.
Meta
November 29, 20260
0
0 solved
Given a BST of ranking scores, replace each node's value with the sum of all values in the tree greater than or equal to it, in place.
A Meta technical-screen BST question where the trick is to traverse in reverse in-order (right, node, left) and carry a running sum. Interviewers check that you do not recompute subtree sums per node.
What the Interviewer Expects
- Identify reverse in-order traversal as descending order
- Carry a single running sum across the traversal
- Update values in place
- Handle negatives and empty trees
- State O(n) time, O(h) space
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Do it with O(1) extra space (Morris traversal).
- Return a new tree without modifying the input.
- What if the tree has duplicate values?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Input: BST root with distinct integer values (clarify duplicates).
- Output: the same tree with
node.val = sum of all original values >= node.val. - Structure must not change....
High-Level Design
Visiting nodes from largest to smallest makes the answer a prefix sum: when we reach a node, we have already seen exactly the values larger than it.
Reverse in-order (right subtree, node, left subtre...