A shop keeps the prices of its items in a binary search tree with distinct values. Given an inclusive band lo..hi, return the sum of every price p with lo <= p <= hi. Return 0 if none qualify.
Examples
20
/ \
10 30
/ \ / \
5 15 25 40
Input: root = build_tree([20, 10, 30, 5, 15, 25, 40]), lo = 10, hi = 25
Output: 70 (10 + 15 + 20 + 25)
46
/ \
28 63
/ \ \
15 37 84
\
24
Input: root = build_tree([46, 28, 63, 15, 37, None, 84, None, 24]), lo = 25, hi = 50
Output: 111 (28 + 37 + 46)
Constraints
- Do not descend into subtrees that lie entirely outside the band
Goals
- Add up only the values inside an inclusive range
- Skip subtrees that cannot contain any value in range