Range Sum of BST

Easy

Company Tags

You are given the root node of a binary search tree and two integers low and high, return the sum of values of all nodes with a value in the inclusive range [low, high].

Example 1:

Input: root = [5,3,8,1,4,7,9,null,2], low = 3, high = 8

Output: 27

Explanation: Nodes 5, 3, 8, 4 and 7 are in the range [3, 8]. 5 + 3 + 8 + 4 + 7 = 27.

Example 2:

Input: root = [4,3,5,2,null], low = 2, high = 4

Output: 9

Explanation: Nodes 4, 3, and 2 are in the range [2, 4]. 4 + 3 + 2 = 9.

Constraints:

  • 1 <= The number of nodes in the tree <= 20,000.
  • 1 <= Node.val, low, high <= 100,000
  • All Node.val are unique.


Company Tags

Please upgrade to NeetCode Pro to view company tags.

root =

low =

high =