LeetCode 255. 验证前序遍历序列二叉搜索树*

LeetCode 255. 验证前序遍历序列二叉搜索树*,第1张

具体思路:

单调栈经典;

如果用左右子树递归判断,会爆;

具体代码:
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;
    }
};

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1353829.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-14
下一篇2022-06-14

发表评论

登录后才能评论

评论列表(0条)

    保存