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

2021-02-03  本文已影响0人  flood_d

0.code

class Solution {
    public boolean verifyPostorder(int[] postorder) {
        if(postorder==null||postorder.length==0){
            return true;
        }
        int len = postorder.length;
        return verifyPostorderHelp(postorder,0,len-1);
    }
    public boolean verifyPostorderHelp(int[] postorder,int start,int end){
        if(start>=end){
            return true;
        }
        int m = start;
        for(int i=start;i<=end-1;i++){
            if(postorder[i]<postorder[end]){
                m=i;
            }else{
                break;
            }
        }
        m++;
        for(int i=m;i<end;i++){
            if(postorder[i]<=postorder[end]){
                return false;
            }
        }
        return verifyPostorderHelp(postorder,start,m-1)&&verifyPostorderHelp(postorder,m,end-1);
    }
}
上一篇下一篇

猜你喜欢

热点阅读