模板类题目,据说今年不会出了。
ez
#include<bits/stdc++.h>
using namespace std;
struct node{int rchild=-1,lchild=-1;
}tree[10];
bool table[10]={};
int in[10],le[10];
int tag1=0,tag2=0;
void inorder(int root){if(root==-1)return;inorder(tree[root].lchild);in[tag1]=root;tag1++;inorder(tree[root].rchild);
}
void levelorder(int root){queue<int> q;q.push(root);while(q.size()!=0){int temp=q.front();q.pop();le[tag2]=temp;tag2++;if(tree[temp].lchild!=-1){q.push(tree[temp].lchild);}if(tree[temp].rchild!=-1){q.push(tree[temp].rchild);}}
}
int main(){int n;scanf("%d",&n);for(int i=0;i<n;i++){char a,b;getchar();scanf("%c %c",&a,&b);if(a>='0'&&a<='9'){tree[i].rchild=a-'0';table[tree[i].rchild]=true;}if(b>='0'&&b<='9'){tree[i].lchild=b-'0';table[tree[i].lchild]=true;}}int root;for(int i=0;i<n;i++){if(table[i]==false) {root=i; break;}}inorder(root);levelorder(root);for(int i=0;i<tag2;i++){printf("%d",le[i]);if(i!=tag2-1) printf(" ");}printf("\n");for(int i=0;i<tag1;i++){printf("%d",in[i]);if(i!=tag1-1) printf(" ");}
}