标签:
#include<iostream> #include<cstdio> #include<cstring> using namespace std; int ld[100010],rd[100010],a,num,root,i; void build(int root,int al) { if(al>root) { if(rd[root]==-1) { rd[root]=al; //cout<<"al:"<<al<<" r root:"<<root<<endl; } else build(rd[root],al); } else { if(ld[root]==-1) { ld[root]=al; //cout<<"al:"<<al<<" l root:"<<root<<endl; } else build(ld[root],al); } } void solve(int root) { if(ld[root]!=-1) { cout<<" "<<ld[root]; solve(ld[root]); } if(rd[root]!=-1) { cout<<" "<<rd[root]; solve(rd[root]); } else return; } int main() { while(~scanf("%d",&num)) { memset(ld,-1,sizeof(ld)); memset(rd,-1,sizeof(rd)); for(i=1;i<=num;i++) { scanf("%d",&a); if(i==1){root=a;} else build(root,a); } cout<<root; solve(root); cout<<endl; } return 0; }
标签:
原文地址:http://www.cnblogs.com/dzzy/p/5071065.html