1 #include <cstdio>
2 #include <cstring>
3 #include <string>
4 #include <queue>
5 #include <stack>
6 #include <iostream>
7 using namespace std;
8 struct node{
9 int l,r,v;
10 };
11 node nodes[1005];
12 void reserve(int num){
13 if(nodes[num].l)
14 reserve(nodes[num].l);
15 if(nodes[num].r)
16 reserve(nodes[num].r);
17 int nn=nodes[num].l;
18 nodes[num].l=nodes[num].r;
19 nodes[num].r=nn;
20 }
21 void prefind(int num){
22 if(!num)
23 return;
24 if(num>1)
25 printf(" ");
26 printf("%d",nodes[num].v);
27 prefind(nodes[num].l);
28 prefind(nodes[num].r);
29 }
30 int main(){
31 //freopen("D:\\INPUT.txt","r",stdin);
32 int n;
33 while(scanf("%d",&n)!=EOF){
34 int i;
35 for(i=1;i<=n;i++){
36 scanf("%d",&nodes[i].v);
37 }
38 //建树
39 for(i=1;i<=n;i++){
40 char c;
41 cin>>c;//对于输入单个字符,不要用scanf!!!
42 //scanf("%c",&c);
43 if(c==‘d‘){
44 scanf("%d %d",&nodes[i].l,&nodes[i].r);
45 }else{
46 if(c==‘l‘){
47 scanf("%d",&nodes[i].l);
48 nodes[i].r=0;
49 }
50 else{
51 if(c==‘r‘){
52 scanf("%d",&nodes[i].r);
53 nodes[i].l=0;
54 }
55 else{//c==‘z‘
56 nodes[i].l=0;
57 nodes[i].r=0;
58 }
59 }
60 }
61 }
62 if(!n){
63 printf("NULL\n");
64 }
65 else{
66 reserve(1);//镜像
67 prefind(1);//前序遍历
68 }
69 }
70 return 0;
71 }