No Extra space solution Java


  • -2
    M
    public boolean verifyPreorder(int[] preorder) {
            if(preorder==null || preorder.length<2)
            return true;
            int min=Integer.MIN_VALUE;
            int j=0;
            for(int i=1;i<preorder.length;i++){
                if(preorder[i]<min)
                return false;
                if(preorder[i]>preorder[i-1]){
                    j=i-1;
                   while(j>=0 && preorder[j]<preorder[i])
                    {   min=preorder[j];
                        preorder[j]=Integer.MAX_VALUE;
                        j--;
                    }
                   
                }
            }
            return true;
        }

Log in to reply
 

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