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

package leetcode;

public class offer_33 {
    public boolean verifyPostorder(int[] postorder) {
        return isPost(postorder, 0, postorder.length-1);
    }
    public boolean isPost(int[] postorder,int left,int right) {
        if(left>=right) {return true;}
        int temp=left;
        //找出当前节点的左右子树分界位置
        while(postorder[temp]<postorder[right]){
            temp=temp+1;
        }
        //记录分界位置
        int mid=temp;
        while(postorder[temp]>postorder[right]) {
            temp=temp+1;
        }
        //temp==right说明符合二叉搜索树
        //递归检查左右子树是否符合
        return temp==right&&isPost(postorder, left, mid-1)&&isPost(postorder, mid, right-1);
    }
}

 

posted on 2022-03-21 17:31  一仟零一夜丶  阅读(26)  评论(0)    收藏  举报