简单题。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std; struct Node
{
int left;
int right;
}s[];
int n;
vector<int>ans1,ans2;
int flag[];
int root; void bfs()
{
queue<int>q; q.push(root);
while(!q.empty())
{
int h=q.front(); q.pop();
ans1.push_back(h);
if(s[h].left!=-) q.push(s[h].left);
if(s[h].right!=-) q.push(s[h].right);
}
} void dfs(int x)
{
if(s[x].left!=-) dfs(s[x].left);
ans2.push_back(x);
if(s[x].right!=-) dfs(s[x].right);
} int main()
{
memset(flag,,sizeof flag);
scanf("%d",&n);
for(int i=;i<n;i++)
{
char L[],R[];
scanf("%s%s",L,R);
if(L[]=='-') s[i].left=-;
else
{
s[i].left=L[]-'';
flag[L[]-'']=;
}
if(R[]=='-') s[i].right=-;
else
{
s[i].right=R[]-'';
flag[R[]-'']=;
}
} for(int i=;i<n;i++)
if(flag[i]==) root=i; for(int i=;i<n;i++)
swap(s[i].left,s[i].right); bfs();
dfs(root);
for(int i=;i<ans1.size();i++)
{
printf("%d",ans1[i]);
if(i<ans1.size()-) printf(" ");
else printf("\n");
} for(int i=;i<ans1.size();i++)
{
printf("%d",ans2[i]);
if(i<ans2.size()-) printf(" ");
else printf("\n");
} return ;
}