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

class Solution {
    public boolean verifySequenceOfBST(int [] sequence) {
        Stack<Integer> stack = new Stack<>();
        int root = Integer.MAX_VALUE;
        for(int i = sequence.length - 1; i >=0; i--){
            if(sequence[i] > root) return false;
            while(!stack.isEmpty() && stack.peek() > sequence[i])
                root = stack.pop();
            stack.add(sequence[i]);
        }
        return true;
    }
}
上一篇:stack 相关


下一篇:1047. 删除字符串中的所有相邻重复项