其他分享
首页 > 其他分享> > 剑指Offer-33.二叉搜索树的后序遍历序列

剑指Offer-33.二叉搜索树的后序遍历序列

作者:互联网

在这里插入图片描述

https://leetcode-cn.com/problems/er-cha-sou-suo-shu-de-hou-xu-bian-li-xu-lie-lcof/solution/mian-shi-ti-33-er-cha-sou-suo-shu-de-hou-xu-bian-6/

法一:递归分治
在这里插入图片描述

//递归
class Solution {
public:
    bool verifyPostorder(vector<int>& postorder) {
         return recur(postorder,0,postorder.size()-1);
    }
    bool recur(vector<int>& postorder,int i,int j){
        if(i>=j)return true;
        int p=i;
        while(postorder[p]<postorder[j])p++;
        int m=p;
        while(postorder[p]>postorder[j])p++;
        return p==j&&recur(postorder,i,m-1)&&recur(postorder,m,j-1);
    }
};

标签:return,recur,Offer,33,bian,二叉,int,xu,postorder
来源: https://blog.csdn.net/qq_41191468/article/details/120150577