Loading content...
You are given the root of a Binary Search Tree (BST) along with two integer boundaries, low and high.
Your task is to compute and return the total sum of all node values that fall within the inclusive range [low, high].
A Binary Search Tree is a tree data structure where for every node:
Utilize the BST property to efficiently traverse only the relevant portions of the tree, avoiding unnecessary exploration of subtrees that cannot contain values within the specified range.
root = [10,5,15,3,7,null,18]
low = 7
high = 1532The nodes with values 7, 10, and 15 all fall within the range [7, 15]. Summing these values: 7 + 10 + 15 = 32.
root = [10,5,15,3,7,13,18,1,null,6]
low = 6
high = 1023The nodes with values 6, 7, and 10 fall within the range [6, 10]. Summing these values: 6 + 7 + 10 = 23.
root = [5]
low = 1
high = 105The tree contains only a single node with value 5. Since 5 is within the range [1, 10], the sum is simply 5.
Constraints