#include <cstdio>
#include <iostream>

using namespace std;


struct TreeNode{
    int data;
    TreeNode* left;
    TreeNode* right;
    TreeNode(int x):data(x),left(NULL),right(NULL){}
};

TreeNode* Insert(TreeNode* root,int x){
    if(root==NULL){
        root = new TreeNode(x);
    }else if(x<root->data){
        root->left= Insert(root->left,x);
    }else if(x>root->data){
        root->right=Insert(root->right,x);
    }
    return root;
}

void PreOrder(TreeNode* T){
    if(T==NULL){
        return;
    }
    printf("%d ",T->data);
    PreOrder(T->left);
    PreOrder(T->right);

    return;
}
void InOrder(TreeNode* T){
    if(T==NULL){
        return;
    }
    InOrder(T->left);
    printf("%d ",T->data);
    InOrder(T->right);

    return;
}

void PostOrder(TreeNode* T){
    if(T==NULL){
        return;
    }
    PostOrder(T->left);
    PostOrder(T->right);
    printf("%d ",T->data);

    return;
}

int main(){
    int sample;
    while(scanf("%d",&sample)!=EOF){
        TreeNode* T=NULL;
        for(int i=0;i<sample;i++){
            int x;
            scanf("%d",&x);
            T=Insert(T,x);
        }

        PreOrder(T);
        printf("\n");
        InOrder(T);
        printf("\n");
        PostOrder(T);
        printf("\n");
    }
    return 0;
}