Skip to content

Instantly share code, notes, and snippets.

class Solution {
public:
bool verifyPreorder(vector<int>& preorder) {
int len = preorder.size(), i = 0, leftBound = INT_MIN;
stack<int> st;
while(i < len)
{
int num = preorder[i];
if(st.size() && num < leftBound)
return false;
if(st.empty() || st.top() > num)
st.push(num);
else
{
while(st.size() && st.top() < num)
{
leftBound = st.top();
st.pop();
}
st.push(num);
}
++i;
}
return true;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment