Sum Account IDs Within a Range in a BST
Last updated: November 29, 2026
Quick Overview
Given the root of a binary search tree of account IDs and an inclusive range [low, high], return the sum of all IDs that fall inside the range.
Meta
November 29, 20260
0
0 solved
Given the root of a binary search tree of account IDs and an inclusive range [low, high], return the sum of all IDs that fall inside the range.
Meta phone screens like short tree problems where the key insight is using the BST property to prune. The interviewer checks that you skip whole subtrees that cannot contain answers rather than visiting every node.
What the Interviewer Expects
- Uses the BST ordering to prune subtrees
- Chooses iterative traversal to avoid recursion depth issues
- Handles inclusive bounds correctly
- States the complexity in terms of visited nodes, not just n
- Handles an empty tree
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
- How would you answer many range-sum queries quickly on a static tree?
- What if the tree is not a BST?
- How would you count, rather than sum, nodes in the range in O(log n) with augmented nodes?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Distinct integer keys, standard BST ordering (left smaller, right larger). Range is inclusive and low <= high. Return 0 for an empty tree or no matches. Aim to visit only nodes that can matter....
High-Level Design
Traverse with an explicit stack. At each node: if its value is in range add it. Only push the left child when node.val > low, because everything on the left is smaller than the node and could still ...