Skip to content

Commit 59dfda6

Browse files
add 938
1 parent c0b239e commit 59dfda6

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Your ideas/fixes/algorithms are more than welcome!
3434
|953|[Verifying an Alien Dictionary](https://leetcode.com/problems/verifying-an-alien-dictionary/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_953.java) | O(1) | O(1) | |Easy|
3535
|944|[Delete Columns to Make Sorted](https://leetcode.com/problems/delete-columns-to-make-sorted/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_944.java) | O(n) | O(1) | |Easy|
3636
|941|[Valid Mountain Array](https://leetcode.com/problems/valid-mountain-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_941.java) | O(n) | O(1) | |Easy|
37+
|938|[Range Sum of BST](https://leetcode.com/problems/range-sum-of-bst/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_938.java) | O(n) | O(n) | |Medium| BST, recursion, DFS
3738
|933|[Number of Recent Calls](https://leetcode.com/problems/number-of-recent-calls/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_933.java) | O(n) | O(n) | |Easy|
3839
|929|[Unique Email Addresses](https://leetcode.com/problems/unique-email-addresses/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_929.java) | O(n) | O(n) | |Easy|
3940
|925|[Long Pressed Name](https://leetcode.com/problems/long-pressed-name/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_925.java) | O(n) | O(1) | |Easy|
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.fishercoder.solutions;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
/**
8+
* 938. Range Sum of BST
9+
*
10+
* Given the root node of a binary search tree, return the sum of values of all nodes with value between L and R (inclusive).
11+
*
12+
* The binary search tree is guaranteed to have unique values.
13+
*
14+
* Example 1:
15+
*
16+
* Input: root = [10,5,15,3,7,null,18], L = 7, R = 15
17+
* Output: 32
18+
*
19+
*
20+
* Example 2:
21+
*
22+
* Input: root = [10,5,15,3,7,13,18,1,null,6], L = 6, R = 10
23+
* Output: 23
24+
*
25+
* Note:
26+
*
27+
* The number of nodes in the tree is at most 10000.
28+
* The final answer is guaranteed to be less than 2^31.
29+
*/
30+
public class _938 {
31+
public static class Solution1 {
32+
public int rangeSumBST(TreeNode root, int L, int R) {
33+
if (root == null) {
34+
return 0;
35+
}
36+
List<Integer> list = new ArrayList<>();
37+
dfs(root, L, R, list);
38+
return list.stream().mapToInt(num -> num).sum();
39+
}
40+
41+
private void dfs(TreeNode root, int l, int r, List<Integer> list) {
42+
if (root == null) {
43+
return;
44+
}
45+
if (root.val <= r && root.val >= l) {
46+
list.add(root.val);
47+
}
48+
dfs(root.left, l, r, list);
49+
dfs(root.right, l, r, list);
50+
}
51+
}
52+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
import com.fishercoder.common.utils.TreeUtils;
5+
import com.fishercoder.solutions._938;
6+
import java.util.Arrays;
7+
import org.junit.BeforeClass;
8+
import org.junit.Test;
9+
10+
import static org.junit.Assert.assertEquals;
11+
12+
public class _938Test {
13+
private static _938.Solution1 solution1;
14+
private static TreeNode root;
15+
16+
@BeforeClass
17+
public static void setup() {
18+
solution1 = new _938.Solution1();
19+
}
20+
21+
@Test
22+
public void test1() {
23+
root = TreeUtils.constructBinaryTree(Arrays.asList(10, 5, 15, 3, 7, null, 18));
24+
TreeUtils.printBinaryTree(root);
25+
assertEquals(32, solution1.rangeSumBST(root, 7, 15));
26+
}
27+
28+
@Test
29+
public void test2() {
30+
root = TreeUtils.constructBinaryTree(Arrays.asList(10, 5, 15, 3, 7, 13, 18, 1, null, 6));
31+
TreeUtils.printBinaryTree(root);
32+
assertEquals(23, solution1.rangeSumBST(root, 6, 10));
33+
}
34+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy