Open zentan66 opened 3 years ago
/**
* @param {TreeNode} root
* @param {number} low
* @param {number} high
* @return {number}
*/
var rangeSumBST = function (root, low, high) {
let result = 0
function dfs(p) {
if (!p) return
dfs(p.left)
if (p.val >= low && p.val <= high) {
result += p.val
}
dfs(p.right)
}
dfs(root)
return result
}
给定二叉搜索树的根结点 root,返回值位于范围 [low, high] 之间的所有结点的值的和。