剑指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/

法一:递归分治
剑指Offer-33.二叉搜索树的后序遍历序列

//递归
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);
    }
};
上一篇:JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-002Table per concrete class with implicit polymorphism(@MappedSuperclass、@AttributeOverride)


下一篇:Django中ORM操作