forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution2.java
40 lines (28 loc) · 1.03 KB
/
Solution2.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
38
39
40
/// Source : https://leetcode.com/problems/validate-binary-search-tree/description/
/// Author : liuyubobobo
/// Time : 2018-05-22
/// Recursive test
/// Time Complexity: O(n)
/// Space Complexity: O(h)
class Solution2 {
public boolean isValidBST(TreeNode root) {
return isValidBST(root, Integer.MIN_VALUE, Integer.MAX_VALUE);
}
private boolean isValidBST(TreeNode node, int min, int max){
if(node == null)
return true;
if(node.val < min || node.val > max)
return false;
if(node.left != null && node.left.val >= node.val)
return false;
if(node.right != null && node.right.val <= node.val)
return false;
return isValidBST(node.left, min, node.val - 1) &&
isValidBST(node.right, node.val + 1, max);
}
public static void main(String[] args){
TreeNode root = new TreeNode(-2147483648);
root.left = new TreeNode(-2147483648);
System.out.println((new Solution2()).isValidBST(root));
}
}