12 Lines Java easy solution


  • 0
    public class Solution {
    public boolean isValidBST(TreeNode root) {
        TreeNode[] pre = {null};
        return helper(root,pre);
    }
    
    public boolean helper(TreeNode node, TreeNode[] pre){
        if(node == null)
            return true;
        if( ! helper(node.left,pre) )
            return false;
        if(pre[0] != null && pre[0].val >= node.val)
            return false;
        pre[0] = node;
        if( !helper(node.right,pre) )
            return false;
        return true;
    }
    

    }


  • 0
    A

    This looks good but I guess you don't need to use an array for pre as you are only storing 1 element at a time.


Log in to reply
 

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