You are given BST root and range [low, high]. Find sum of values in range.
Approach
Add in-range values; prune subtrees that cannot contain any.
Time O(n)Space O(h)
How it works
1Add the node when it falls inside [low, high].
2Skip the left subtree when node <= low; skip right when node >= high.
Sample input / output
Input:
7 7 15
10 5 15 3 7 x 18
Output:
32
Constraints
1 <= nodes <= 2 * 10^4; 1 <= low <= high <= 10^5.
Solution — read it, then walk it
Time O(n)Space O(h)Loops 1Branches 8Returns 4source · Python
Scroll sideways for wide charts — the view follows the active step.
start / end
statement
condition
loop
returns
definition
1/43
StartSTART
Interactive control-flow diagram and step-by-step code walkthrough.
Recommended resourcesAffiliate links · may earn us a commission
Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books →Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses →Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits →