-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path98. Validate Binary Search Tree.java
70 lines (59 loc) · 1.98 KB
/
98. Validate Binary Search Tree.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
* Definition for a binary tree node.
* public 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;
* }
* }
*/
class Solution {
public boolean isValidBST(TreeNode root) {
return simpleCheck(root, Long.MIN_VALUE, Long.MAX_VALUE);
// return isBST(root).isbst;
}
boolean simpleCheck(TreeNode root, long start, long end) {
if (root == null) return true ;
if (start < root.val && root.val < end) {
boolean leftTree = simpleCheck(root.left, start, root.val);
boolean rightTree = simpleCheck(root.right, root.val, end);
return leftTree && rightTree;
}
return false;
}
class State {
int minimum;
int maximum;
boolean isbst;
State() {
this.minimum = Integer.MAX_VALUE;
this.maximum = Integer.MIN_VALUE;
this.isbst = true;
}
State(int min, int mx, boolean bst) {
this.minimum = min;
this.maximum = mx;
this.isbst = bst;
}
}
State isBST(TreeNode root) {
if (root == null) return new State();
// faith
State left = isBST(root.left);
State right = isBST(root.right);
if (left.isbst == false || right.isbst == false) return new State(Integer.MAX_VALUE, Integer.MIN_VALUE, false);
boolean isbst = (left.maximum < root.val && root.val < right.minimum) ? true : false ;
if (isbst == false) return new State(Integer.MAX_VALUE, Integer.MIN_VALUE, false);
State state = new State();
state.minimum = Math.min(left.minimum, right.minimum);
state.maximum = Math.min(left.maximum, right.maximum);
state.isbst = isbst;
return state;
}
}