T.C. DFS O(n)
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @param {number} low
* @param {number} high
* @return {number}
*/
var rangeSumBST = function(root, low, high) {
let ans = 0;
dfs(root, low, high);
return ans;
function dfs(root, low, high) {
if(root) {
if(low <= root.val && root.val <= high) {
ans += root.val;
}
if(low < root.val) {
dfs(root.left, low, high);
}
if(root.val < high) {
dfs(root.right, low, high);
}
}
}
};