My simple c++ solutions


  • 1
    K
    class Solution {
    public:
    bool verifyPreorder(vector<int>& arr) {
        stack<int>stack;
        int min=INT_MIN;
        if(arr.size()<=1)return true;
        stack.push(arr[0]);
        for(int i=1;i<arr.size();i++)
        {
            if(arr[i]<=min)return false;
            while( !stack.empty()&&arr[i]>stack.top())
            {
                min=stack.top();
                stack.pop();
            }
            stack.push(arr[i]);
        }
        return true;
    }
    

    };


Log in to reply
 

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