Description

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].

Examples

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

7 + 10 + 15 = 32.

Input:root = [8,3,10,1,6,null,14,null,null,4,7,13,null], low = 5, high = 12
Output:31
Explanation:

Values in range [5,12]: 6, 7, 8, 10. Sum = 6 + 7 + 8 + 10 = 31.

Input:root = [20,10,30,5,15,25,35], low = 18, high = 32
Output:75
Explanation:

Values within range [18,32]: 20 + 25 + 30 = 75. Node 10 is less than 18, nodes 5 and 15 are less than 18, and node 35 is greater than 32, so they are excluded.

Constraints

  • 1 ≤ nodes ≤ 2 × 10⁴

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!