We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 80a12e6 commit 0525365Copy full SHA for 0525365
javascript/0938-range-sum-of-bst.js
@@ -0,0 +1,31 @@
1
+/**
2
+ * Definition for a binary tree node.
3
+ * function TreeNode(val, left, right) {
4
+ * this.val = (val===undefined ? 0 : val)
5
+ * this.left = (left===undefined ? null : left)
6
+ * this.right = (right===undefined ? null : right)
7
+ * }
8
+ */
9
10
+ * DFS | Recursion
11
+ * Time O(n) | Space O(n)
12
+ * https://leetcode.com/problems/range-sum-of-bst
13
+ *
14
+ * @param {TreeNode} root
15
+ * @param {number} low
16
+ * @param {number} high
17
+ * @return {number}
18
19
+var rangeSumBST = function(root, low, high) {
20
+
21
+ let total = 0;
22
23
+ const dfs = (node) => {
24
+ if(!node) return;
25
+ if(node.val >= low && node.val <= high) total += node.val;
26
+ dfs(node.left);
27
+ dfs(node.right);
28
+ }
29
+ dfs(root);
30
+ return total;
31
+};
0 commit comments