Given a binary tree, determine if it is a valid binary search tree (BST).
原来还可以用null =_=|||...
public boolean isValidBST(TreeNode root) {
if (root == null) {
return true;
}
return helper(root, null, null);
}
private boolean helper(TreeNode root, Integer min, Integer max) {
if (root == null) {
return true;
}
if ((min != null && min >= root.val) || (max != null && max <= root.val)) {
return false;
}
boolean left = helper(root.left, min, root.val);
boolean right = helper(root.right, root.val, max);
return left && right;
}
public boolean isValidBST(TreeNode root) {
if (root == null) {
return true;
}
return helper(root, Long.MIN_VALUE, Long.MAX_VALUE);
}
private boolean helper(TreeNode root, Long min, Long max) {
if (root == null) {
return true;
}
if (min >= root.val || max <= root.val) {
return false;
}
boolean left = helper(root.left, min, (long) root.val);
boolean right = helper(root.right, (long) root.val, max);
return left && right;
}
public boolean isValidBST(TreeNode root) {
if (root == null) {
return true;
}
Result r = new Result();
isValidBSTHelper(root, r);
return r.isBST;
}
private void isValidBSTHelper(TreeNode root, Result r) {
if (root == null || r.isBST == false) {
return;
}
isValidBSTHelper(root.left, r);
if (r.isBST == false) {
return;
}
if (r.last != null && r.last.val >= root.val) {
r.isBST = false;
return;
}
r.last = root;
isValidBSTHelper(root.right, r);
}
class Result {
TreeNode last;
Boolean isBST = true;
}