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

提示:
数组长度 <= 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.push(postorder[i]); } return true; } }

浙公网安备 33010602011771号