递归分治,不断划分,从后向前找第一个比最后一个大的,然后用他划分,分成两个,继续递归。
public class Solution {
public boolean VerifySquenceOfBST(int [] sequence) {
if(sequence.length == 0){
return false;
}
return check(sequence,0,sequence.length-1);
}
public boolean check(int[] we,int low,int high){
if(low >= high ){
return true;
}
int li = high-1;
for(;we[li] > we[high];){
li--;
if(li == 0){
break;
}
}
for(int i =0;i<li;i++){
if(we[i]>we[high])
return false;
}
return check(we,0,li) && check(we,li+1,high-1);
}
}