Range Sum of BST
EasyTreesbst
Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.
Practice this as a mock interviewFree to start, no account neededProblem
Given the root of a binary search tree with unique values and two integers low and high, return the sum of every node value v with low <= v <= high (both bounds inclusive).
Return 0 if no value falls in the range. Use the BST property to skip subtrees that cannot contain values inside the range.
Examples
Input: root = [10,5,15,3,7,null,18], low = 7, high = 15
Output: 32
7 + 10 + 15 = 32.
Input: root = [10,5,15,3,7,13,18,1,null,6], low = 6, high = 10
Output: 23
6 + 7 + 10 = 23.
Constraints
- 1 <= number of nodes <= 2 * 10^4
- 1 <= Node.val <= 10^5
- All values are unique and form a valid BST.
- 1 <= low <= high <= 10^5
What the interviewer grades
A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.