Count BST nodes in a range

Given a binary search tree root, and integers lo and hi, return the count of all nodes in root whose values are between lo and hi (inclusive).

For example, with this tree and lo = 5 and hi = 10, return 3 since only 7, 8, 9 are between [5, 10].

   3
  / \
 2   9
    / \
   7   12
  / \
 4   8

Example 1

Input

root = [3, null, [5, null, null]]
lo = 4
hi = 7

Output

1

Last updated

Was this helpful?