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

题要

输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历结果。如果是则返回true,否则返回false。假设输入的数组的任意两个数字都互不相同。

难点

  1. 单支树输入:{1,2,3,4,5}
  2. 对于仅有单孩子节点的

递归分治

解法一


递归出口:

  1. 有单个子节点或无子节点 :r - l <= 1, 则return true
  2. 右子树区间内的所有节点都应大于根节点,否则return false
class Solution {
    public boolean verifyPostorder(int[] postorder) {
        int l = postorder.length;
        if(l==0) return true;
        return recur(postorder, 0, postorder.length-1);        
    }

    public boolean recur(int[] postorder, int l, int r){
        if (r - l <= 1) return true;//注意输出条件为r - l <= 1,可能有单个子节点也有可能有无子节点
        int f = l;
        while(f<r&&postorder[f]<postorder[r]){//对于{1,2,3,4}的输入,则循环结果为f==r
            f++;
        }
        for(int j=f; j<r; j++){//false的判定条件,对于f==r则不会进行此循环
            if(postorder[j]<postorder[r]){
                return false;
            }
        }
        return recur(postorder, l, f-1)&&recur(postorder, f, r-1);

    }
}

解法二

递归出口:

  1. 若无子节点则i>j;若有单个子节点则i==j
  2. 每一层递归中是否从头遍历到尾

class Solution {
    public boolean verifyPostorder(int[] postorder) {
        return recur(postorder, 0, postorder.length - 1);
    }
    boolean recur(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);
        //若p!=j即右子树有不符合二叉搜索树条件的节点
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值