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

时间:2021-09-05 18:44:28   收藏:0   阅读:17

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

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

参考以下这颗二叉搜索树:

     5
    /    2   6
  /  1   3

示例 1:

输入: [1,6,3,2,5]
输出: false

示例 2:

输入: [1,3,2,6,5]
输出: true

提示:

解题思路:

解题之前,要先明晰一些基本概念。

递归思路

个人当初做这道题的思路如下:

首先想到划分左右子树,然后再判断是否为二叉搜索树。

在判断左右子树的时候,设置一个temp依次遍历完整左子树,然后等大于根节点的树的时候直接break。

然后再创建rightTreeNode再遍历右子树,如果最后postorder[i]等于根节点的树,遍历结束。

最后在判断rightTreeNode与end是否相同,且要用&&,必须使rightTreeNode == end、recur(postorder,start,temp)、recur(postorder,temp + 1,end - 1)都是true才能返回。

class Solution {
    public boolean verifyPostorder(int[] postorder) {
        return recur(postorder,0,postorder.length - 1);
    }
    boolean recur(int[] postorder, int start, int end){
        if(start >= end) return true;
        int temp = start; 
        // 找到右子树结点第一次出现的地方。(或者说是遍历完整棵左子树)
        for(int i = start; i <= end; ++i){
            if(postorder[i] < postorder[end]){
                temp = i;
            }
            else break;
        }
        int rightTreeNode = temp + 1; // 后序遍历右子树时会访问的第一个结点的下标。
        // 验证右子树所有结点是否都大于根结点。
        for(int i = rightTreeNode; i <= end; ++i){
            if(postorder[i] > postorder[end])
                ++rightTreeNode;
        }
        return rightTreeNode == end && recur(postorder,start,temp) && recur(postorder,temp + 1,end - 1);
    }
}

这个是K神更为简洁的代码的算法思路:

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);
    }
}

参考链接:

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/

原文:https://www.cnblogs.com/RainsX/p/15229459.html

评论(0)
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!