File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments