public class Solution {
public boolean VerifySquenceOfBST(int [] sequence) {
if(sequence.length == 0) return false;
return test(sequence,0,sequence.length-1);
}
private boolean test(int[] sequence,int head,int tail){
//当节点为一个时返回true
if(head >= tail) return true;
int j;
int mid = sequence[tail];
//找到左右树的分界 从后往前循环
for(j = tail; j >= head; j--){
int cur = sequence[j];
//当cur小于mid时找到中间值
if(mid > cur) break;
}
//判断左子树中是否符合搜索树条件
for(int i = j; i>=head;i--){
int cur = sequence[i];
//当cur大于mid即不是搜索树,返回false
if(cur > mid) return false;
}
return test(sequence,head,j) && test(sequence,j+1,tail-1);
}
}
06-23
558
11-10
155
11-08
261
11-08
474