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

本问题对应课程链接:剑指offer刷题第四期

要求:

1、请打卡你的解题代码,如果能简单说一下思路则简单说一下思路。
2、给出你代码所用的时间复杂度和空间复杂度

共以下 30 个回答

  • ppopp 普通 2023年8月20日 下午2:33

    //遍历 ,对每一个根节点判断是否满足二叉搜索树后序序列
    //左右根,根据根的值确定左右子树结点,然后计算包括左子节点前所有的结点都小于根,右子节点都大于根
    //基于数组操作的
    class Solution {
    public:
    bool verifyPostorder(vector& postorder) {
    bool flg = true;
    int len = postorder.size();
    for(int i=0; i < len; i++){
    flg = check(postorder, i);
    if(!flg) return false;
    }
    return true;
    }

    bool check(vector<int>& postorder, int pos){
        //value
        int cur = postorder[pos];
        int p = 0;
        //定位,p停在第一个大于cur的值上
        while(postorder[p] < cur){
            p++;
        }
        for(int i = p; i < pos; i++){
            if(postorder[i] < cur) return false;
        }
        return true;
    }
    

    };

    0 赞同 0 条回复
  • hi 普通 2023年8月10日 上午9:52
    1. 跟二叉树重建那题递归方面有相似之处
    class Solution {
    public:
        //数组名变为全局变量后,数组可以在两个函数中使用
        vector<int> post;
    
        bool verifyPostorder(vector<int>& postorder) {
        //二叉搜索树具有左子树都比根节点小,右子树都比根节点大的特点
            post = postorder;
            if(postorder.empty()) return true;
            return dfs(0, postorder.size() - 1);
        }
    
        bool dfs(int l, int r){
            if(l >= r) return true;
            int root = post[r];
            int k = l;
            for(int i = l; i < r; i++)
                if(post[i] < root) k++;
            //k 为第一个大于root 的数
            for(int i = k ; i < r; i++)
                if(post[i] < root) return false;
            return dfs(l, k - 1) && dfs(k, r - 1);
        }
    };
    
    0 赞同 0 条回复
  • 山川湖海 普通 2023年8月3日 下午9:54

    class Solution {
    public:
    bool traversal(vector& postorder, int start, int end) {
    /* 递归终止条件 /
    if(start >= end) return true;
    int index = start;
    /
    中间处理逻辑 /
    while(postorder[index] < postorder[end]) index++;
    /
    记录分割点 /
    int midIndex = index; //
    while(postorder[index] > postorder[end]) index++;
    /
    递归左右子树 */
    bool left = traversal(postorder, start, midIndex – 1);
    bool right = traversal(postorder, midIndex, end – 1);
    // index end判断是否为二叉搜索树
    return index
    end && left && right;
    }

    bool verifyPostorder(vector<int>& postorder) {
        return traversal(postorder, 0, postorder.size()-1);
    }
    

    };

    0 赞同 0 条回复
  • doordie. 普通 2023年7月27日 下午3:30
    class Solution {
        public boolean verifyPostorder(int[] postorder){
                // 递归参数:后序遍历序列/左边界/右边界
                return recur(postorder,0,postorder.length-1);
            }
    
        private boolean recur(int[] postorder, int left, int right) {
                // 1.终止条件:左边界 》= 右边界
                if(left >= right){
                    return true;
                }
                // 2.划分左右子树
    
                int rootValue = postorder[right]; // 当前树根节点的值
    
                int ptr = left;
                // 找出第一个大于根节点的节点  索引为ptr 此时可以划分出左子树区间是[left,ptr-1]   右子树区间[ptr,right-1]
                while (postorder[ptr] < postorder[right]){
                    ptr++;
                }
                // 3.判断是否为二叉搜索树
                // 进行判断后续区域是否所有的值都大于当前的根节点,如果出现小于的值就直接返回
                for (int i = ptr; i < right; i++){
                    if (postorder[i] < rootValue){
                        return false;
                    }
                }
                // 若当前树没问题就检查左右子树
                if(!recur(postorder,left,ptr-1)){
                    return false;
                }
                if (!recur(postorder,ptr,right-1)){
                    return false;
                }
                return true;
    
        }
    }
    
    0 赞同 0 条回复
  • 小季同学 普通 2023年7月27日 上午9:43

    ts

    function verifyPostorder(postorder: number[]): boolean {
        // 边界判断
        if(postorder === null) {
            return true
        }
        return foo(postorder, 0, postorder.length - 1)
    };
    
    function foo(postorder: number[], i: number, j: number): boolean {
        // 递归结束
        if(i >= j) {
            return true
        }
        const root = postorder[j]
        let p = i
        // 获取第一个大于root元素的位置
        while(postorder[p] < root){
            p++
        }
        // 如果右子树存在小于root元素,返回false
        for(let k = p; k < j; k++) {
            if(postorder[k] < root) {
                return false
            }
        }
    
        // 递归
        return foo(postorder, i, p - 1) && foo(postorder, p, j - 1)
    
    }
    
    0 赞同 0 条回复
  • 今天你刷题了嘛 普通 2023年7月22日 下午4:04
    class Solution {
        public boolean verifyPostorder(int[] postorder) {
            // 根据后序,看能否构造出一棵二叉搜索树
            if(postorder==null||postorder.length<=0){
                return true;
            }
            int root = postorder[postorder.length-1];
            return recur(postorder,0,postorder.length-1);
        }
    
        public boolean recur(int[] postorder,int left,int right){
            if(left>=right) return true;
            int root = postorder[right];
            int index = right;
            boolean isleft, isright;
            for(int i=0;i<right;i++){
                if(postorder[i]>root){
                    index = i;
                    break;
                }
            }
            // 二叉搜索树的定义:左子树的元素都小于根,右子树的元素都大于根
            // 判断右子树是否有小于root的元素
            for(int i=index;i<right;i++){
                if(postorder[i]<root) return false;
            }
            isleft = recur(postorder,left,index-1);
            isright = recur(postorder,index,right-1);
            return isleft&&isright;
        }
    }
    

    时间复杂度:O(n^2)
    空间复杂度:O(n)

    0 赞同 0 条回复
  • CKX 普通 2023年7月22日 下午3:50
    func verifyPostorder(postorder []int) bool {
        return traverse(postorder, 0, len(postorder) - 1)
    }
    
    func traverse(postorder []int, i int, j int) bool {
        if i >= j {
            return true
        }
        root := postorder[j]
    
        // postorder[i..left) 是左子树,应该都小于 root
        left := i
        for left < j && postorder[left] < root {
            left++
        }
    
        // postorder[left..j) 是右子树,应该都大于 root
        right := left
        for right < j && postorder[right] > root {
            right++
        }
        if right != j {
            return false
        }
        return traverse(postorder, i, left-1) && traverse(postorder, left, j-1)
    }
    
    0 赞同 0 条回复
  • 北辰 普通 2023年7月21日 下午7:46
    public class 二叉搜索树的后序遍历序列 {
    
        public static void main(String[] args) {
            int[] postorder = {1,3,2,6,5};
            System.out.println(verifyPostorder(postorder));
        }
    
        public static boolean verifyPostorder(int[] postorder) {
            int n = postorder.length;
            int i=0,j=n-1;
            if(i>=j) return true;
    
            return myVerifyPostorder(postorder,i,j);
        }
    
        private static boolean myVerifyPostorder(int[] postorder, int i, int j) {
            if(i>=j) return true;
            int num=postorder[i];
            int k=i;
            while(postorder[k]<postorder[j]) k++;
            int m = k;
            while(postorder[k]>postorder[j]) k++;
            return k==j&&myVerifyPostorder(postorder,i,m-1)&&myVerifyPostorder(postorder,m,j-1);
        }
    }
    

    时间复杂度:O(n2)
    空间复杂度:O(n)

    0 赞同 0 条回复
  • 片言莫语 普通 2023年7月20日 下午5:12
    class Solution {
        public boolean verifyPostorder(int[] postorder) {
            if(postorder == null){
                return true;
            }
    
            return f(postorder, 0, postorder.length - 1);
        }
    
        boolean f(int[] postorder, int i, int j){
            if(i >= j){
                return true;
            }
    
            int root = postorder[j];
            int p = i;
            // 获取第一个大于或者等于 root 等元素的位置
            while(postorder[p] < root) p++;
            // 判断 p ~ j -1 这个范围是否存在小于root的元素
            for(int k = p; k < j; k++){
                if(postorder[k] < root){
                    return false;
                }
            }
    
            return f(postorder, i, p - 1) && f(postorder, p, j - 1);
        }
    }
    
    0 赞同 0 条回复
  •  普通 2023年7月19日 下午10:57

    c++代码,递归分治的思想
    class Solution {
    public:
    bool verifyPostorder(vector& postorder)
    {
    return istrue(postorder,0,postorder.size()-1);
    }
    bool istrue(vector
    & postorder,int start,int end)
    {
    if(start>=end) return true;
    int index=start;
    while(postorder[index]<postorder[end])index++;//找到第一个大于根节点的值
    int te=index;
    while(postorder[index]>postorder[end]) index++;//判断右子树是否大于根节点
    return indexend&&istrue(postorder,start,te-1)&&istrue(postorder,te,end-1);
    }
    };

    0 赞同 0 条回复
  • 枫梓。 普通 2023年7月19日 下午2:16
    class Solution {
    private:
        bool helper(vector<int> postorder, int left, int right) {
            if(left >= right) return true; // ==为一个结点, >为没有结点,均为true的条件。
            int mid = left;
            int root = postorder[right];
            while(postorder[mid] < root) {
                mid++;
            } //现在mid指向第一个大于root值的结点、
            int temp = mid;
            while(temp < right){
                if(postorder[temp] < root) return false;
                ++temp;
            }
            return helper(postorder, left, mid - 1) && helper(postorder, mid, right - 1);
        }
    
    public:
        bool verifyPostorder(vector<int>& postorder) {
            return helper(postorder, 0, postorder.size() - 1);
        }
    };
    

    递归栈是O(n) 时间复杂度也为O(n)

    0 赞同 0 条回复
  • Marshan 普通 2023年7月18日 下午11:58

    递归实现

    class Solution {
    public:
        bool verifyPostorder(vector<int>& postorder) {
            if(postorder.size() == 0){
                return true;
            }
            return f(postorder,0,postorder.size()-1);
        }
        bool f(vector<int>& postorder,int i,int j){
            if(i >= j){
                return true;
            }
            int root = postorder[j];
            int p = i;
            while(postorder[p] < root)p++;
            for(int k = p;k < j;++k){
                if(postorder[k] < root){
                    return false;
                }
            }
            return f(postorder,i,p-1) && f(postorder,p,j-1);
        }
    };
    
    0 赞同 0 条回复
  • Mby 普通 2023年7月18日 下午11:49
    class Solution {
        public boolean verifyPostorder(int[] postorder) {
            if(postorder == null){
                return true;
            }
            return f(postorder, 0, postorder.length - 1);
    
        }
    
        boolean f(int[] postorder, int i, int j){
            if(i >= j){
                return true;
            }
    
            int root = postorder[j];
            int p = i;
            //获取第一个大于或等于root 元素的位置
            while(postorder[p] < root)p++;
            //判断p到j-1的范围是否存在小于root的元素
            for(int k = p; k < j; k++){
                if(postorder[k] < root){
                    return false;
                }
            }
            return f(postorder, i, p - 1)&&f(postorder, p, j - 1);
        }
    }
    
    0 赞同 0 条回复
  • 土豆芋泥包_ 普通 2023年7月18日 下午9:01

    代码

    class Solution {
        public boolean verifyPostorder(int[] postorder) {
            if(postorder==null){
                return true;
            }
            return f(postorder,0,postorder.length-1);
        }
    
        boolean f(int[] postorder, int i, int j){
            if(i>=j){
                return true;
            }
            int root = postorder[j];
            int p=i;
            while(postorder[p]<root) p++;
            for(int k=p;k<j;k++){
                if(postorder[k]<root){
                    return false;
                }
            }
            return f(postorder,i,p-1)&&f(postorder,p,j-1);
        }
    
    }
    
    0 赞同 0 条回复
  • mpweixin用户 普通 2023年7月18日 下午3:48
    public boolean verifyPostorder(int[] postorder) {
    //        // 单调栈使用,单调递增的单调栈
    //        Deque<Integer> stack = new LinkedList<>();
    //        int pervElem = Integer.MAX_VALUE;
    //        // 逆向遍历,就是翻转的先序遍历
    //        for (int i = postorder.length - 1;i>=0;i--){
    //            // 左子树元素必须要小于递增栈被peek访问的元素,否则就不是二叉搜索树
    //            if (postorder[i] > pervElem){
    //                return false;
    //            }
    //            while (!stack.isEmpty() && postorder[i] < stack.peek()){
    //                // 数组元素小于单调栈的元素了,表示往左子树走了,记录下上个根节点
    //                // 找到这个左子树对应的根节点,之前右子树全部弹出,不再记录,因为不可能在往根节点的右子树走了
    //                pervElem = stack.pop();
    //            }
    //            // 这个新元素入栈
    //            stack.push(postorder[i]);
    //        }
    //        return true;
    
            return dfs(postorder, 0, postorder.length - 1);
        }
    
            // 输入: [1,3,2,6,5]
        public boolean dfs(int[] postorder,int left ,int right){
            if (left == right || Math.abs(right - left) == 1) {
                return true;
            }
            int temp = postorder[right];
            int index = right;
            for (int i = left; i < right; i++) {
                if (postorder[i] > temp) {//表示找到了左边的左子树了
                    index = i;
                    //我现在判断一下右边的是不是都大于他
                    for (int j = i; j < right; j++) {
                        if (postorder[j] < temp) {
                            return false;
                        }
                    }
                    break;
                }
            }
            return dfs(postorder, left, index - 1) && dfs(postorder, index, right - 1);
        }
    
    0 赞同 0 条回复
  • 人生何处不青山 普通 2023年7月18日 下午12:59

    TS写的

    function verifyPostorder(postorder: number[]): boolean {
        const dfs = (l: number, r: number) => {
            if (l >= r) return true;
            let x = l;
            while (postorder[x] < postorder[r]) x++;
            let y = x;
            while (postorder[y] > postorder[r]) y++;
            return y === r && dfs(l, x - 1) && dfs(x, r - 1);
        }
        return dfs(0, postorder.length - 1);
    };
    
    0 赞同 0 条回复
  • CaCO3 永久会员 2023年7月18日 上午11:06

    ”’java
    class Solution {
    public boolean verifyPostorder(int[] postorder) {
    //二叉搜索树中根结点的值大于左子树中任何一个结点的值,小于右子树中任何一个结点的值,子树也是
    return verify(postorder,0,postorder.length-1);
    }
    //递归
    private boolean verify(int[] postorder,int left,int right){
    if(left >= right){
    return true;
    }
    //最后一个结点是根结点
    int root = postorder[right];
    //先寻找右子树
    int rightNode = right;
    for(int i = right-1;i >= left;i–){
    if(postorder[i] < root){
    rightNode = i;
    break;
    }
    }
    rightNode = rightNode right ? left:rightNode+1;
    for(int i = left;i < rightNode;i++){
    if(postorder[i] > root){
    return false;
    }
    }
    boolean flagRight = verify(postorder,rightNode,right-1);
    boolean flagLeft = verify(postorder,left,rightNode-1);
    return flagRight && flagLeft;

    }
    

    }
    时间复杂度O(n)
    空间复杂度O(1)
    ”’

    0 赞同 0 条回复
  • AD钙奶 普通 2023年7月17日 下午10:31
    class Solution {
        public boolean verifyPostorder(int[] postorder) {
            return traversal(postorder,0,postorder.length - 1);
        }
    
        public boolean traversal(int[] postorder, int l, int r) {
            if(l >= r) return true;
            int rootVal = postorder[r];
            int index = l; //分割点下标
            while(postorder[index] < rootVal) {
                index ++;
            }
            int temp = index;
            while(temp < r) {
                if(postorder[temp] < rootVal) {
                    return false;
                }
                temp ++;
            }
            return traversal(postorder,l,index - 1) && traversal(postorder, index, r - 1);
        } 
    }
    
    0 赞同 0 条回复
  • ONE PIECE 普通 2023年7月17日 下午9:33
    class Solution {
        public boolean verifyPostorder(int[] postorder) {
            return rescue(postorder,0,postorder.length-1);
        }
        public boolean rescue(int[] postorder,int left,int right){
            if(left >= right) return true;
            int p =left;
            while(postorder[p] < postorder[right]) p++;
            int m = p;
            while(postorder[p] > postorder[right]) p++;
            return p==right && rescue(postorder,left,m-1) && rescue(postorder,m,right-1);
        }
    }
    

    时间复杂度: O(nlog(n))

    0 赞同 0 条回复
  • June 普通 2023年7月17日 下午9:31
    func verifyPostorder(postorder []int) bool { //分治法
        if len(postorder) == 0 {
    
            return true
        }
        if len(postorder) == 1 {
            return true
        }
    
        var ans bool = true
        var mid int = 0
        var n int = len(postorder)
        var flag int = postorder[n-1]
        var isvis bool = false
        for i:=0 ; i < n-1 ; i++ {
            if i == 0 && postorder[i] > flag {
                mid = i
                isvis = true
            }else if i == n-2 && postorder[i] < flag {
                if isvis == true {
                    return false
                }
                mid = n-1
                isvis = true
            }else if postorder[i] < flag && flag < postorder[i+1] && isvis == false{
                mid = i+1
                isvis = true
            }else if postorder[i] < flag && flag < postorder[i+1] && isvis == true {
                return false  //如果重复出现分裂点则肯定不对
            }
    
        }
    
        ans = ans && verifyPostorder(postorder[0:mid]) && verifyPostorder(postorder[mid:n-1])
        return ans
    }
    
    
    0 赞同 0 条回复
  • NullPointerException 普通 2023年7月17日 下午8:08

    class Solution {
    public boolean verifyPostorder(int[] postorder) {
    //二叉搜索树 左子树比根节点小 右子树一定比根节点大
    //后序遍历 左右根 小 大 小
    //可以倒序 变成 根右左 先找第一个小于它 归为右子树 后面变成左子树
    if(postordernull||postorder.length0)
    {
    return true;
    }
    return verifyPostorder1(postorder,0,postorder.length-1);

    }
    public boolean verifyPostorder1(int []postorder,int left,int right)
    {
        if(left>right||left==right)
        {
            return true;
        }
        int root=postorder[right];
        int k=left;
        for(int i=right-1;i>=left;i--)
        {
            if(postorder[i]<root)
            {
                k=i;//找到第一个比根小的,那么那边为左子树,i 到 右边为右子树
                break;
            }
        }
        for(int i=left;i<k;i++)//一定比 root 小
        {
            if(postorder[i]>root) return false;
        }
        return verifyPostorder1(postorder,left,k)&&verifyPostorder1(postorder,k+1,right-1);
    
    
    }
    

    }

    0 赞同 0 条回复
  • #00456B 普通 2023年7月17日 下午6:12

    思路

    首先需要了解后序遍历,数组的最后一个元素就是根节点,前面分成两个部分,先是左子树然后是右子树。
    这道题目用的是BST,BST的特点就是左子树的数值都比root的数值校,右子树都比root大。

    所以一个正确的BST的后序遍历数组应该是:从头开始遍历找到的第一个比root大的数,前面的都是左子树,之后到最后一个元素之前都是右子树。

    据此,可以写出以下代码。由于子树中的所有节点的值都小于root,但是子树中还有子树,所以需要用递归来判断所有的节点。

    代码

    class Solution {
    public:
        bool verifyPostorder(vector<int>& postorder) {
            return check(postorder, 0, postorder.size() - 1);
        }
        bool check(vector<int>& postorder, int i, int j){//i,j 分别为左边界和右边界的下标
            //终止条件
            if(i >= j){
                return true;
            }
            int root = postorder[j];
            //因为后序遍历是左右中,所以左子树和右子树的边界都比较好定义,一个参数就够了
            int left = i;//左子树的边界
            while(left < j && postorder[left] < root){//BST的左子树都比root小
                left++;
            }
            int right = left;//右子树的左边界从左子树的右边界+1开始,之前的left++了,所以left这时候就是右子树的左边界
            while(right < j && postorder[right] > root){
                right++;
            }
            if(right != j){//如果right不能遍历到数组的最后一位,说明数组不满足BST的后序遍历
                return false;
            }
            return check(postorder, i, left - 1) && check(postorder, left, j - 1);
        }
    };
    
    0 赞同 0 条回复
  • Mooby 普通 2023年7月17日 下午5:54

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

    0 赞同 0 条回复
  • 艺 普通 2023年7月17日 下午5:19

    “`c++
    //递归
    class Solution {
    public:
    bool dfs(vector& nums,int l,int r)
    {
    if(l>=r) return true;
    int i=l;
    while(nums[i]nums[r]) i++;
    return i==r&&dfs(nums,l,j-1)&&dfs(nums,j,r-1);
    }
    bool verifyPostorder(vector
    & postorder) {
    return dfs(postorder,0,postorder.size()-1);
    }
    };

    0 赞同 0 条回复
  • 长河呼叫黄江 普通 2023年7月17日 上午2:01

    Day9:下面是代码

    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 q = p;
            while(postorder[q] > postorder[j]) q++;
            return q == j && recur(postorder, i, q - 1) && recur(postorder, q, j - 1);
        }
    }
    
    0 赞同 0 条回复
  • Luminous 普通 2023年7月16日 下午11:57
    class Solution {
        public boolean verifyPostorder(int[] postorder) {
            return check(postorder,0,postorder.length-1);
        }
    
        public boolean check(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&&check(postorder,i,m-1)&&check(postorder,m,j-1);
        }
    }
    

    时间复杂度:O(nlogn)
    空间复杂度:O(n)

    0 赞同 0 条回复
  • wonderful 普通 2023年7月16日 下午11:45

    通过单调栈,从后往前左子树一直大于根节点
    class Solution:
    def verifyPostorder(self, postorder: List[int]) -> bool:
    stack, root = [], float(“+inf”)
    for i in range(len(postorder) – 1, -1, -1):
    if postorder[i] > root: return False
    while(stack and postorder[i] < stack[-1]):
    root = stack.pop()
    stack.append(postorder[i])
    return True
    O(N)
    O(N)

    0 赞同 0 条回复
  • 周小二 普通 2023年7月16日 下午10:49

    代码如下

    class Solution {
        private int[] postorder;
        public boolean verifyPostorder(int[] postorder) {
            this.postorder = postorder;
            return recur(0, postorder.length - 1);
        }
    
        private boolean recur(int left, int right) {
            if (left >= right) {
                return true;
            }
            int root = postorder[right];
            int i = left;
            while (postorder[i] < root) {
                i++;
            }
            int m = i;
            while (postorder[i] > root) {
                i++;
            }
            return i == right && recur(left, m - 1) && recur(m, right - 1);
        }
    }
    
    0 赞同 0 条回复
  • 阿尚 普通 2023年7月16日 下午8:48
    class Solution {
        public boolean verifyPostorder(int[] postorder) {
            if(postorder == null || postorder.length == 0) return true;
            return process(postorder, 0, postorder.length - 1);
        }
    
        public boolean process(int[] postorder, int begin, int end){
            if(begin > end) return true;
            //找左右子树边界
            int border = 0;
            while(postorder[border] < postorder[end]){
                border++;
            }
    
            for(int i = border; i < end; i++){
                if(postorder[i] < postorder[end]) return false;
            }
    
            boolean left = process(postorder, begin, border - 1);
            boolean right = process(postorder, border, end - 1);
    
            return left && right;
        }
    }
    
    0 赞同 0 条回复
  • 波雅汉库克 普通 2023年7月16日 下午4:14
    class Solution {
    public:
        bool isBST(vector<int>& pre, int start, int end){
            if(start >= end){
                return true;
            }
            int root = pre[end-1];
            int index = start;
            for(; index < end-1; index++){
                if(pre[index] > root){
                    break;
                }
            }
            //index is the first element bigger than root val
            for(int i = index; i<end-1; i++){
                if(pre[i] < root){
                    return false;
                }
            }
            return isBST(pre,start,index) && isBST(pre,index,end-1);
    
    
        }
        bool verifyPostorder(vector<int>& postorder) {
            if(postorder.size()==0 || postorder.size()==1){
                return true;
            }
            int len = postorder.size();
            return isBST(postorder,0,len);
    
        }
    };
    

    时间复杂度:O(n^2)
    空间复杂度:O(n)

    1. 利用栈
    class Solution {
    public:
        bool verifyPostorder(vector<int>& postorder) {
            if(postorder.size()==0 || postorder.size()==1){
                return true;
            }
            stack<int> stk;
            int root = INT_MAX;
            for(int i=postorder.size()-1; i>=0; i--){
                int cur = postorder[i];
                while(!stk.empty() && stk.top() > cur){
                    root = stk.top();
                    stk.pop();
                }
                if(cur > root){
                    return false;
                }
                stk.push(cur);
            }
            return true;
        }
    };
    

    时间复杂度:O(n)
    空间复杂度:o(n)

    0 赞同 0 条回复

# 回答此问题

后才能回答