diff --git a/README.md b/README.md index 46167fa..f48180b 100644 --- a/README.md +++ b/README.md @@ -209,6 +209,7 @@ - [901: Stock plan](problems/leetcode/901) - Monotonic stack - [905: Sort array by parity](problems/leetcode/905) - Easy - Array - [907: Sum of subarray minimums](problems/leetcode/907) - Monotonic stack +- [938: Range sum of BST](problems/leetcode/938) - Easy - Tree - [941: Valid mountain array](problems/leetcode/941) - Easy - Array - [947 - Most stones removed with same row or column](problems/leetcode/947) - [976: Largest perimeter triangle](problems/leetcode/976) diff --git a/problems/leetcode/938/solution.js b/problems/leetcode/938/solution.js new file mode 100644 index 0000000..f955352 --- /dev/null +++ b/problems/leetcode/938/solution.js @@ -0,0 +1,25 @@ +/** + * 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} + */ +const rangeSumBST = (root, low, high) => { + let sum = 0; + if (root) { + if (low <= root.val && root.val <= high) { + sum += root.val; + } + sum += rangeSumBST(root.left, low, high) + sum += rangeSumBST(root.right, low, high); + } + return sum; +}