Skip to the code
cf.
+10 XP

#337 Revenue Inside a Price Range

easy

A sales report lets the user drag a price slider from low to high. Sold prices are stored in a binary search tree with unique values.

Write rangeTotal(root, low, high) that returns the sum of all values v with low <= v <= high. Skip subtrees that can't contain any value in the range. If nothing is in range, return 0.

Example 1

Input: root = [10,5,15,3,7,null,18], low = 7, high = 15
Output: 32

7 + 10 + 15 = 32.

Example 2

Input: root = [10,5,15,3,7,null,18], low = 19, high = 30
Output: 0

No price is that high.

Constraints

  • 0 <= number of prices <= 20000
  • 1 <= low <= high <= 10^5

Trees are written level by level, like [1, 2, 3, null, 4], where null is a missing child. Your function works with TreeNode objects, each with val, left, and right.

Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).