- Range Sum of BST
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]
.
var rangeSumBST = function(root, low, high) {
let sum = 0
function preOrder(node) {
if (!node) {
return
}
if (node.val >= low && node.val <= high) {
sum += node.val
}
preOrder(node.left)
preOrder(node.right)
}
preOrder(root)
return sum
}