一道很好的拓展题,对于follow up,我们之后继续看。
参考:点击打开链接
public class Solution {
public boolean verifyPreorder(int[] preorder) {
int min = Integer.MIN_VALUE;
Stack<Integer> stack = new Stack<>();
for (int i: preorder) {
if (i < min) {
return false;
}
while (!stack.isEmpty() && i > stack.peek()) {
min = stack.pop();
}
stack.push(i);
}
return true;
}
}