注意,层序遍历那个也要是翻转后的
#include<bits/stdc++.h>
using namespace std;
struct node{
int left,right;
}a[100];
bool f[100];
int n;
vector<int>level,in;
int convert(string s)
{
int ans=0;
if(isdigit(s[0]))
{
for(int i=0;i<s.length();i++)ans=ans*10+s[i]-'0';
}
else ans=-1;
return ans;
}
void BFS(int root)
{
queue<int>q;
q.push(root);
while(!q.empty())
{
int front=q.front();
level.push_back(front);
q.pop();
if(a[front].right!=-1)q.push(a[front].right);
if(a[front].left!=-1)q.push(a[front].left);
}
}
void show(vector<int>a)
{
for(int i=0;i<a.size();i++)
{
printf("%d",a[i]);
if(i!=a.size()-1)printf(" ");
}
printf("\n");
}
void inorder(int root)
{
if(root==-1)return;
inorder(a[root].right);
in.push_back(root);
inorder(a[root].left);
}
int main()
{
scanf("%d",&n);
for(int i=0;i<n;i++)
{
string s1,s2;
cin>>s1>>s2;
a[i].left=convert(s1);
if(a[i].left>=0)f[a[i].left]=true;
a[i].right=convert(s2);
if(a[i].right>=0)f[a[i].right]=true;
}
int root;
for(int i=0;i<n;i++)
{
if(f[i]==false)
{
root=i;
break;
}
}
BFS(root);
show(level);
inorder(root);
show(in);
return 0;
}