-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode938.java
More file actions
34 lines (29 loc) · 1.09 KB
/
LeetCode938.java
File metadata and controls
34 lines (29 loc) · 1.09 KB
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
34
import util.TreeNode;
public class LeetCode938 {
public static void main(String[] args) {
// 输入:root = [10,5,15,3,7,null,18], low = 7, high = 15
// 输出:32
System.out.println(
new Solution938().rangeSumBST(TreeNode.buildTree(new Integer[] { 10, 5, 15, 3, 7, null, 18 }), 7, 15));
// 输入:root = [10,5,15,3,7,13,18,1,null,6], low = 6, high = 10
// 输出:23
System.out.println(
new Solution938().rangeSumBST(TreeNode.buildTree(new Integer[] { 10, 5, 15, 3, 7, 13, 18, 1, null, 6 }),
6, 10));
}
}
class Solution938 {
public int rangeSumBST(TreeNode root, int low, int high) {
return dfs(root, low, high);
}
public int dfs(TreeNode root, int low, int high) {
if (root == null) {
return 0;
}
int sum = 0;
sum += root.val <= high && root.val >= low ? root.val : 0;
sum += root.val >= low ? dfs(root.left, low, high) : 0;
sum += root.val <= high ? dfs(root.right, low, high) : 0;
return sum;
}
}