Java recursive O(n) solution


  • 0
    A
    public class Solution {
        int index = 0;
        public boolean verifyPreorder(int[] preorder) {
            return helper(preorder, Integer.MIN_VALUE, Integer.MAX_VALUE);
        }
        
         public boolean helper(int[] preorder, int min, int max) {
            if (index == preorder.length)
                return true;
            if (preorder[index] > min && preorder[index] < max) {
                int data = preorder[index];
                index++;
                if (helper(preorder, min, data))
                    return true;
                else
                    return helper(preorder, data, max);
            }
            return false;
        }
    }
    

Log in to reply
 

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