即如果把中序序列当做栈的压入序列,那么后序序列是该栈的一个弹出序列
import java.util.*;
public class Solution {
public boolean VerifySquenceOfBST(int [] sequence) {
int[] arr = sequence.clone();
Arrays.sort(arr);
return IsPopOrder(arr,sequence);
}
public boolean IsPopOrder(int[] arr,int[] seq){
Stack<Integer> stack = new Stack<>();
if(arr.length==0||seq.length==0){
return false;
}
int Index = 0;
for(int i=0;i<arr.length;i++){
stack.push(arr[i]);
while(!stack.isEmpty() && stack.peek()==seq[Index]){
stack.pop();
Index++;
}
}
return stack.isEmpty();
}
}