forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_938.java
30 lines (26 loc) · 841 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
package com.fishercoder.solutions;
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 l, int r, List<Integer> list) {
if (root == null) {
return;
}
if (root.val <= r && root.val >= l) {
list.add(root.val);
}
dfs(root.left, l, r, list);
dfs(root.right, l, r, list);
}
}
}