-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_938.java
33 lines (30 loc) · 979 Bytes
/
_938.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package com.fishercoder.solutions.firstthousand;
import com.fishercoder.common.classes.TreeNode;
import java.util.ArrayList;
import java.util.List;
public class _938 {
public static class Solution1 {
public int rangeSumBST(TreeNode root, int low, int high) {
if (root == null) {
return 0;
}
List<Integer> list = new ArrayList<>();
dfs(root, low, high, list);
return list.stream().mapToInt(num -> num).sum();
}
private void dfs(TreeNode root, int low, int high, List<Integer> list) {
if (root == null) {
return;
}
if (root.val <= high && root.val >= low) {
list.add(root.val);
}
if (root.val > low) {
dfs(root.left, low, high, list);
}
if (root.val < high) {
dfs(root.right, low, high, list);
}
}
}
}