-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.java
37 lines (29 loc) · 883 Bytes
/
Solution.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 L98;
import javax.swing.tree.TreeNode;
public class Solution {
private class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() {
}
TreeNode(int val) {
this.val = val;
}
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
public boolean isValidBST(TreeNode root) {
return check(root, -(1l << 31), (1l << 31) - 1);
}
private boolean check(TreeNode root, long rangeLeft, long rangeRight) {
if (root == null) return true;
if (root.val < rangeLeft || root.val > rangeRight) {
return false;
}
return check(root.left, rangeLeft, (long) root.val - 1) && check(root.right, (long) root.val + 1, rangeRight);
}
}