Validate Binary Search Tree

Java代码

public boolean isValidBST(TreeNode root) {
    return isValidBST(root, Integer.MAX_VALUE, Integer.MIN_VALUE);
}

public boolean isValidBST(TreeNode root, int max, int min) {
    if (root == null)
        return true;
    if (root.val <= min || root.val >= max)
        return false;

    return isValidBST(root.left, Math.min(max, root.val), min) && isValidBST(root.right, max, Math.max(min, root.val));
}

results matching ""

    No results matching ""