1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<stack>
4 using namespace std;
5 struct node
6 {
7 int data;
8 node *left,*right;
9 };
10 void insert(node *a,node *b)
11 {
12 if(a->data>b->data)
13 {
14 if(!a->left)a->left=b;
15 else insert(a->left,b);
16 }
17 if(a->data<b->data)
18 {
19 if(!a->right)a->right=b;
20 else insert(a->right,b);
21 }
22 }
23 void pre(node *a)
24 {
25 stack<node*>s;
26 node *p=a;
27 while(p||!s.empty())
28 {
29 while(p)
30 {
31 printf("%d ",p->data);
32 s.push(p);
33 p=p->left;
34 }
35 if(!s.empty())
36 {
37 p=s.top(),s.pop();
38 p=p->right;
39 }
40 }
41 }
42 void in(node *a)
43 {
44 stack<node*> s;
45 node *p=a;
46 while(p||!s.empty())
47 {
48 while(p)
49 {
50 s.push(p);
51 p=p->left;
52 }
53 if(!s.empty())
54 {
55 p=s.top(),s.pop();
56 printf("%d ",p->data);
57 p=p->right;
58 }
59 }
60 }
61 void post(node *a)
62 {
63 stack<node*> s;
64 node *pre;
65 while(a||!s.empty())
66 {
67 if(a) s.push(a),a=a->left;
68 else
69 {
70 a=s.top();
71 if(a->right&&pre!=a->right) a=a->right;
72 else a=pre=s.top(),printf("%d ",a->data),s.pop(),a=0;
73 }
74 }
75
76 }
77 main()
78 {
79 int n,i,k;
80 while(~scanf("%d",&n))
81 {
82 node *root=(node*)malloc(sizeof(node));
83 scanf("%d",&root->data),root->left=0,root->right=0;
84 for(i=1;i<n;i++)
85 {
86 node *p=(node*)malloc(sizeof(node));
87 scanf("%d",&p->data),p->left=0,p->right=0;
88 insert(root,p);
89 }
90 pre(root),printf("\n"),in(root),printf("\n"),post(root),printf("\n");
91 }
92 }