/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean isValidBST(TreeNode root) {
return helper(root, Long.MIN_VALUE, Long.MAX_VALUE);
}
private boolean helper(TreeNode root, long left, long right) {
if (root == null) {
return true;
}
if (root.val > left && root.val < right) {
if (root.left != null && !helper(root.left, left, root.val)) {
return false;
}
if (root.right != null && !helper(root.right, root.val, right)) {
return false;
}
return true;
} else {
return false;
}
}
}
Validate Binary Search Tree
最新推荐文章于 2020-10-15 09:59:45 发布