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;
} else if (root.val > left && root.val < right) {
if (!helper(root.left, left, root.val)) {
return false;
}
if (!helper(root.right, root.val, right)) {
return false;
}
return true;
} else {
return false;
}
}
}
Validate Binary Search Tree
最新推荐文章于 2024-10-14 16:31:29 发布