File tree 2 files changed +35
-0
lines changed 2 files changed +35
-0
lines changed Original file line number Diff line number Diff line change 747
747
935|[ Knight Dialer] ( ./solutions/0935-knight-dialer.js ) |Medium|
748
748
936|[ Stamping The Sequence] ( ./solutions/0936-stamping-the-sequence.js ) |Hard|
749
749
937|[ Reorder Data in Log Files] ( ./solutions/0937-reorder-data-in-log-files.js ) |Medium|
750
+ 938|[ Range Sum of BST] ( ./solutions/0938-range-sum-of-bst.js ) |Easy|
750
751
966|[ Vowel Spellchecker] ( ./solutions/0966-vowel-spellchecker.js ) |Medium|
751
752
970|[ Powerful Integers] ( ./solutions/0970-powerful-integers.js ) |Easy|
752
753
976|[ Largest Perimeter Triangle] ( ./solutions/0976-largest-perimeter-triangle.js ) |Easy|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 938. Range Sum of BST
3
+ * https://leetcode.com/problems/range-sum-of-bst/
4
+ * Difficulty: Easy
5
+ *
6
+ * Given the root node of a binary search tree and two integers low and high, return the sum
7
+ * of values of all nodes with a value in the inclusive range [low, high].
8
+ */
9
+
10
+ /**
11
+ * Definition for a binary tree node.
12
+ * function TreeNode(val, left, right) {
13
+ * this.val = (val===undefined ? 0 : val)
14
+ * this.left = (left===undefined ? null : left)
15
+ * this.right = (right===undefined ? null : right)
16
+ * }
17
+ */
18
+ /**
19
+ * @param {TreeNode } root
20
+ * @param {number } low
21
+ * @param {number } high
22
+ * @return {number }
23
+ */
24
+ var rangeSumBST = function ( root , low , high ) {
25
+ return traverse ( root ) ;
26
+
27
+ function traverse ( node ) {
28
+ if ( ! node ) return 0 ;
29
+ const value = node . val ;
30
+ if ( value < low ) return traverse ( node . right ) ;
31
+ if ( value > high ) return traverse ( node . left ) ;
32
+ return value + traverse ( node . left ) + traverse ( node . right ) ;
33
+ }
34
+ } ;
You can’t perform that action at this time.
0 commit comments