#include <stdio.h>
#include <stdlib.h>
bool isSequence(int *array, int start,int end) {
if(start > end ){
return false;
}
if(start == end){
return true;
}
int root = array[end-1];
int i = start;
while( array[i] < root && i < end-1){//第一个大于根的数的位置position,为右子树的开始
i++;
}
int position = i;
while( i<end-1) {
if(array[i] < root) { //如果在position后还有小于根的数,则不是二叉查找树
return false;
}
i++;
}
//若左右子树有一个不为二叉查找树,则当前序列不满足条件
bool left = true;
left = isSequence(array,start,position);
bool right = true;
right = isSequence(array,position,end-1);
return (left && right);
}
main(){
int a[7] = {5,7,6,9,11,10,8};
int b[] = {7,4,6,5};
printf("%d\n",isSequence(a,0,6));
printf("%d\n",isSequence(b,0,3));
system("pause");
return 0;
}