#include <iostream>
using namespace std;
typedef struct btree{
int data;
struct btree *left;
struct btree *right;
}btree;
btree *insert(btree *t,int x)
{
if(t==NULL)
{
t=new btree;
t->data=x;
t->left=t->right=NULL;
}
if(x<t->data)
t->left=insert(t->left,x);
if(x>t->data)
t->right=insert(t->right,x);
return t;
}
void preorder(btree *t)
{
if(t==NULL)
return;
cout<<t->data<<" ";
preorder(t->left);
preorder(t->right);
}
void inorder(btree *t)
{
if(t==NULL)
return;
inorder(t->left);
cout<<t->data<<" ";
inorder(t->right);
}
void postorder(btree *t)
{
if(t==NULL)
return;
postorder(t->left);
postorder(t->right);
cout<<t->data<<" ";
}
int main() {
int n;
while(cin>>n)
{btree *root=NULL;
while(n--)
{
int x;cin>>x;
root=insert(root,x);
}
preorder(root);
cout<<endl;
inorder(root);
cout<<endl;
postorder(root);
cout<<endl;
}
}