No
推荐指数:※
来源:http://ac.jobdu.com/problem.php?pid=1367
二叉搜索树的左子树都比更小,右子数都比根大。
#include<iostream> #include<stdio.h> #include<stdlib.h> #include<string.h> using namespace std; bool rebuild_tree(int low,int high,long long *arr) { if(low>high) return true; int i,index=high; long long tmp=arr[high]; for(i=low;i<high;i++){ if(arr[i]>tmp){ index=i; break; } } for(i=index+1;i<high;i++){ if(arr[i]<tmp) return false; } return (rebuild_tree(low,index-1,arr)==true&&rebuild_tree(index+1,high-1,arr)==true); } int main() { int n,i; while(scanf("%d",&n)!=EOF){ long long *arr=new long long [n]; for(i=0;i<n;i++) scanf("%lld",&arr[i]); if(rebuild_tree(0,n-1,arr)==false) printf("No\n"); else printf("Yes\n"); } return 0; }