Open pwstrick opened 4 years ago
938. 二叉搜索树的范围和
/** * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; * this.left = this.right = null; * } */ /** * @param {TreeNode} root * @param {number} L * @param {number} R * @return {number} */ var sum; var rangeSumBST = function(root, L, R) { sum = 0; dfs(root, L, R); return sum; }; function dfs(root, L, R) { if(root == null) { return; } if(root.val >= L && root.val <= R) { sum += root.val; } dfs(root.left, L, R); dfs(root.right, L, R); }
938. 二叉搜索树的范围和