// editor3
#include<stdio.h>
#include<stdlib.h>

typedef struct node{
    int data;
    struct node*left;
    struct node*right;
}node;
node*root=NULL;

node*create(int num){
    node*newnode=(node*)malloc(sizeof(node));
    newnode->data=num;
    newnode->left=NULL;
    newnode->right=NULL;
    return newnode;
}

node*insert(node*root,int r){
    if(root==NULL){
        return create(r);
    }
    if(root-left==NULL){
        root->left=insert(root->left,r);
    }
    else if(root->right==NULL){
        root->right=insert(root->right,r);
    }
    else{
        root->left=insert(root->left,r);
    }
    return root;
}

void postorder(node*root){
    if(root!=NULL){
        postorder(root->left);
        postorder(root->right);
        printf("%d ",root->data);
    }
}

void preorder(node*root){
    if(root!=NULL){
        printf("%d ",root->data);
        preorder(root->left);
        preorder(root->right);
    }
}

void inorder(node*root){
    if(root!=NULL){
        inorder(root->left);
        printf("%d ",root->data);
        inorder(root->right);
    }
}

int main(){
    int n,r;
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d",&r);
        root = insert(root,r);
    }
    if(n<0){
        printf("Invalid input");
        return 0;
    }
    preorder(root);
    printf("\n");
    inorder(root);
    printf("\n");
    postorder(root);
    return 0;
}