Skip to content

Commit 0aba783

Browse files
committedMar 27, 2025
Add solution #938
1 parent 509cb07 commit 0aba783

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,7 @@
747747
935|[Knight Dialer](./solutions/0935-knight-dialer.js)|Medium|
748748
936|[Stamping The Sequence](./solutions/0936-stamping-the-sequence.js)|Hard|
749749
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|
750751
966|[Vowel Spellchecker](./solutions/0966-vowel-spellchecker.js)|Medium|
751752
970|[Powerful Integers](./solutions/0970-powerful-integers.js)|Easy|
752753
976|[Largest Perimeter Triangle](./solutions/0976-largest-perimeter-triangle.js)|Easy|

‎solutions/0938-range-sum-of-bst.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
};

0 commit comments

Comments
 (0)
Please sign in to comment.