-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRangeSumOfBST.java
37 lines (30 loc) · 968 Bytes
/
RangeSumOfBST.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
34
35
36
37
package com.smlnskgmail.jaman.leetcodejava.easy;
import com.smlnskgmail.jaman.leetcodejava.support.TreeNode;
// https://leetcode.com/problems/range-sum-of-bst/
public class RangeSumOfBST {
private final TreeNode root;
private final int low;
private final int high;
public RangeSumOfBST(TreeNode root, int low, int high) {
this.root = root;
this.low = low;
this.high = high;
}
public int solution() {
return count(root, low, high);
}
private int count(TreeNode head, int low, int high) {
if (head == null) {
return 0;
}
if (head.val >= low && head.val <= high) {
return head.val
+ count(head.left, low, high)
+ count(head.right, low, high);
} else if (head.val < low) {
return count(head.right, low, high);
} else {
return count(head.left, low, high);
}
}
}