![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/470ee83800fb507cd86f589e01ba30a8.png)
原题链接
- 首先定义一个栈
- 定义最大数root
- 进行操作,将数组的值从后向前遍历
- 若出现当前的值大于root的情况,则直接return false;因为这样是不满足后序遍历要求的
- 判断当栈非空,并且stack.peek() > postorder[i] 的时候,将root置为stack.pop();
- 否则直接将stack.add(postorder[i])
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;
}
}