98.Validate Binary Search Tree

Solution 1: accepted

In-order traversal.

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
class Solution {
private int last = Integer.MIN_VALUE;
private boolean first = true; // for special case [-2147483648]
public boolean isValidBST(TreeNode root) {
if (root == null) {
return true;
}
if (!isValidBST(root.left)) {
return false;
}
if (!first && last >= root.val) {
return false;
}
first = false;
last = root.val;
if (!isValidBST(root.right)) {
return false;
}
return true;
}
}