#include <iostream>
#include <queue>
using namespace std;
struct node{
int lchild,rchild;
}no[20];
int N,root;
bool hashtable[20]={0};
void levelorder(){
queue<int> q;
q.push(root);
int count=0;
while(!q.empty()){
int t=q.front();
q.pop();
printf("%d",t);
if(++count!=N) printf(" ");
if(no[t].lchild!=-1) q.push(no[t].lchild);
if(no[t].rchild!=-1) q.push(no[t].rchild);
}
}
int countn=0;
void inorder(int root){
if(root==-1) return;
inorder(no[root].lchild);
++countn!=N?printf("%d ",root):printf("%d",root);
inorder(no[root].rchild);
}
int main(){
scanf("%d",&N);
for(int i=0;i<N;i++){
char str1[5],str2[5];
scanf("%s%s",str1,str2);
if(str1[0]!='-'){
no[i].rchild=str1[0]-'0';
hashtable[str1[0]-'0']=true;
}else no[i].rchild=-1;
if(str2[0]!='-'){
no[i].lchild=str2[0]-'0';
hashtable[str2[0]-'0']=true;
}else no[i].lchild=-1;
}
for(int i=0;i<N;i++)
if(hashtable[i]==false) root=i;
levelorder();
printf("\n");
inorder(root);
return 0;
}
PAT A1102
最新推荐文章于 2022-11-15 21:11:22 发布