具体思路:
单调栈经典;
如果用左右子树递归判断,会爆;
具体代码:
class Solution {
public:
bool verifyPreorder(vector<int>& preorder) {
stack<int>st;
int minx=INT_MIN;
preorder.push_back(INT_MAX);
for(auto& val:preorder){
if(val<minx)
return false;
while(!st.empty()&&val>st.top()){
minx=st.top();
st.pop();
}
st.push(val);
}
return true;
}
};