/**
* 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) {
return false;
}
return helper(root.left, left, root.val) && helper(root.right, root.val, right);
}
}
Validate Binary Search Tree
最新推荐文章于 2022-04-27 14:17:49 发布