5
0 0
4 5
1 2
0 0
0 0
样例输出
3 1 2 4 5
1 3 4 2 5
1 4 5 2 3
1 3 5 1 1
对于100%的数据:1<=n<=10^5。 90分挑错
#include<bits/stdc++.h>
using namespace std;
struct node
{
long long data,dad,l,r;
}t[1000009];
long long cnt;
void a(long long u){
if(u==0){
return ;
}
printf("%lld ",t[u].data);
a(t[u].l);
a(t[u].r);
}
void b(long long u)
{
if(u==0)
return ;
b(t[u].l);
printf("%lld ",t[u].data);
b(t[u].r);
}
void c(long long u)
{
if(u==0)
return ;
c(t[u].l);
c(t[u].r);
printf("%lld ",t[u].data);
}
void find(long long u){
if(u==0){
return ;
}
cnt++;
find(t[u].l);
find(t[u].r);
}
int main()
{
long long v,l,r,n;
scanf("%lld",&n);
for(long long i=1;i<=n;i++){
t[i].dad=-1;
}
for(long long i=1;i<=n;i++){
scanf("%lld%lld",&l,&r);
t[i].data=i;
t[i].l=l;
t[i].r=r;
if(l!=0){
t[l].dad=i;
}
if(r!=0){
t[r].dad=i;
}
}
int start;
for(long long i=1;i<=n;i++){
if(t[i].dad==-1){
start=t[i].data;
}
}
//cout<<root<<endl;
a(start);
cout<<endl;
b(start);
cout<<endl;
c(start);
cout<<endl;
for(long long i=1;i<=n;i++){
cnt=0;
find(t[i].data);
//cout<<i<<endl;
printf("%lld ",cnt);
}
return 0;
}