You are given the root node of a binary search tree and two integers minVal
and maxVal
. Your task is to find the sum of all node values that lie within the inclusive range [minVal, maxVal]
in the tree.
Example 1:
Input: tree = [12,7,20,4,10,null,25], minVal = 8, maxVal = 20 Output: 42 Explanation: Nodes with values 10, 12, and 20 are within the range [8, 20]. Their sum is 10 + 12 + 20 = 42.
Example 2:
Input: tree = [8,3,15,1,6,12,18,null,null,4,7], minVal = 5, maxVal = 15 Output: 50 Explanation: Nodes with values 6, 7, 8, 12, and 15 fall within the range [5, 15]. Their sum is 6 + 7 + 8 + 12 + 15 = 48.
1
and 25000
.1
and 120000
.1 ≤ minVal ≤ maxVal ≤ 120000