Skip to content

Latest commit

 

History

History
22 lines (21 loc) · 673 Bytes

98.md

File metadata and controls

22 lines (21 loc) · 673 Bytes

#98. Validate Binary Search Tree 题目链接

public class Solution {
    public boolean isValidBST(TreeNode root) {
        /*
            确定一个结点的上界和下界,通过比较来确定结点是否合法
         */
        return valid(root, Long.MIN_VALUE, Long.MAX_VALUE);
    }

    public boolean valid(TreeNode root, long min, long max) {
        if (root == null) {
            return true;
        }
        if (root.val <= min || root.val >= max) {
            return false;
        }
        return valid(root.left, min, root.val) && valid(root.right, root.val, max);
    }
}