#include <iostream>
using namespace std;
struct BiOTree{
    int data;
    struct BiOTree* leftchild;
    struct BiOTree* rightchild;
    BiOTree(int a):data(a),leftchild(NULL),rightchild(NULL){};
};
BiOTree* BuildT(BiOTree* root,int x){
    if(root==NULL){
        root=new BiOTree(x);
    }
    else if(x<root->data){
        root->leftchild=BuildT(root->leftchild, x);
    }
    else if(x>root->data){
        root->rightchild=BuildT(root->rightchild, x);
    }
    return root;
}
void MidOrder(BiOTree* root){
    if(root==NULL) return;
    MidOrder(root->leftchild);
    cout<<root->data<<" ";
    MidOrder(root->rightchild);
}
void PreOrder(BiOTree* root){
    if(root==NULL) return;
    cout<<root->data<<" ";
    PreOrder(root->leftchild);
    PreOrder(root->rightchild);
}
void PostOrder(BiOTree* root){
    if(root==NULL) return;
    PostOrder(root->leftchild);
    PostOrder(root->rightchild);
    cout<<root->data<<" ";
}
int main() {
    int n;
    while(cin>>n){
        BiOTree* root=NULL;
        for(int i=0;i<n;i++){
            int tem;
            cin>>tem;
            root=BuildT(root, tem);
        }
        PreOrder(root);
        cout<<endl;
        MidOrder(root);
        cout<<endl;
        PostOrder(root);
        cout<<endl;


    }
}