Given a pre order, find whether it is single child BST

#include <stdio.h>
#include <limits.h>

int isSingleChildBST(int a[], int n){

int mx = INT_MAX, i;

for(i = 1; i < n ; ++i){

if(a[i] <= a[i - 1]){ //assuming equal elements are in left tree
mx = a[i - 1];
}else{
if(a[i] > mx)
return 0;
}
}

return 1;

}

int main(){

int a[] = {10,19,17,14,15,16};

printf("%s\n", isSingleChildBST(a, 6) ? "YES" : "NO");
return 0;
}
Advertisements