剑指offer(23)二叉搜索树的后序遍历序列
public class Solution {
//二叉搜索树的后续遍历
//后序排序:数组的最后一个节点为头节点
//搜索二叉树:左子树比根节点小,右子树比根节点大
public boolean VerifySquenceOfBST(int [] sequence) {
int length = sequence.length;
if(length == 0){
return false;
}
return isRight(sequence,0,length-1);
}
public boolean isRight(int[] sequence, int start , int end){
if(start >= end){
return true;
}
int i = end - 1;
while(sequence[i] > sequence[end] && i > start){
i--;
}
for(int j = start; j < i;j++){
if(sequence[j] > sequence[end]){
return false;
}
}
return isRight(sequence , start , i) && isRight(sequence , i + 1 , end-1 );
}
}