二叉搜索树的后序遍历序列
输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则输出Yes,否则输出No。假设输入的数组的任意两个数字都互不相同。
public class Solution {
public boolean VerifySquenceOfBST(int [] sequence) {
int len = sequence.length;
if(sequence == null || len == 0){
return false;
}
int[] leftSequence = new int[len-1];
int[] rightSequence = new int[len-1];
int root = sequence[len-1];
int i = 0;
for (;i<len-1;i++){
if(sequence[i]<root) {
leftSequence[i] = sequence[i];
}
else{
break;
}
}
int j = i;
for (;j<len-1;j++){
if (sequence[j]>root) {
rightSequence[j - i] = sequence[j];
}
if(sequence[j]<root){
return false;
}
}
boolean left = true;
if(i>0){
left=VerifySquenceOfBST(leftSequence);
}
boolean right = true;
if(i<len-1){
right =VerifySquenceOfBST(rightSequence);
}
return (left&&right);
}
}