排到rank3 ,有点意外,纪念一下
3 | leo | 62MS | 972K | 1555B | C++ | 2011-09-03 23:11:19 |
#include <list>
#include <map>
#include <set>
#include <queue>
#include <string>
#include <deque>
#include <stack>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <limits.h>
#include <time.h>
#include <string.h>
using namespace std;
#define LL long long
#define PI acos(-1.0)
#define MAX INT_MAX
#define MIN INT_MIN
#define eps 1e-8
#define FRE freopen("a.txt","r",stdin)
#define MOD 1000000007
#define N 100010
int l[N],r[N];
void insert(int rt,int u){
int i,j;
if(u>rt){
if(r[rt]==-1)
r[rt]=u;
else
insert(r[rt],u);
}
else{
if(l[rt]==-1)
l[rt]=u;
else
insert(l[rt],u);
}
}
void print(int rt){
int i,j;
if(l[rt]!=-1){
printf(" %d",l[rt]);
print(l[rt]);
}
if(r[rt]!=-1){
printf(" %d",r[rt]);
print(r[rt]);
}
}
int main(){
int n;
while(scanf("%d",&n)!=EOF){
int i,j;
memset(l,-1,sizeof(l));
memset(r,-1,sizeof(r));
int rt,tmp;
for(i=0;i<n;i++){
scanf("%d",&tmp);
if(!i){
rt=tmp;
}
else
insert(rt,tmp);
}
printf("%d",rt);
print(rt);
puts("");
}
return 0;
}