A clean and simple java solution.


  • -1
    N
    public boolean isValidBST(TreeNode root) {
        return helper(root,Integer.MAX_VALUE,Integer.MIN_VALUE);
    }
    public boolean helper(TreeNode root, int max, int min){
        if(root == null)
            return true;
        if(root.val>=max||root.val<=min)
            return false;
        if(root.left!=null){
            if(root.val<=root.left.val||!helper(root.left,root.val,min))
                return false;
        }
        if(root.right!=null){
           if(root.val>=root.right.val||!helper(root.right,max,root.val))
                return false;
            
        }
        return true;
    }

  • 0
    K

    not work for input = {2147483647}


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.