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

时间:2022-07-25
本文章向大家介绍【剑指Offer】二叉搜索树的后序遍历序列,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

题目

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

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

 5
/ 

2 6 / 1 3 示例 1:

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

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

提示:

数组长度 <= 1000

题解

class Solution {
    public boolean verifyPostorder(int[] postorder) {
        Stack<Integer> stack = new Stack<>();
        int root = Integer.MAX_VALUE;
        for(int i = postorder.length - 1; i >= 0; i--) {
            if(postorder[i] > root) return false;
            while(!stack.isEmpty() && stack.peek() > postorder[i])
            	root = stack.pop();
            stack.add(postorder[i]);
        }
        return true;
    }
}