#include<stdio.h>
#include<stdlib.h>

typedef struct Node{
    int data;
    struct Node *left,*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 num){
    if(root == NULL)return create(num);
    if(root->left == NULL){
        root->left = insert(root->left,num);
    }
    else if(root->right == NULL){
        root->right = insert(root->right,num);
    }
    else{
        root->left = insert(root->left,num);
    }
    return root;
}

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);
    }
}

void postOrder(node* root){
    if(root!=NULL){
        postOrder(root->left);
        postOrder(root->right);
        if(root->data == 0) continue;
        printf("%d ",root->data);
    }
}



int main(){
    int n,num;
    scanf("%d",&n);
    if(n<=0){
        printf("Invalid input");
        return 0;
    }
    for(int i=0;i<n;i++){
        scanf("%d",&num);
        if(num == 0) continue;
        if(num<0){
            printf("Invalid input");
            return 0;
        }
        root = insert(root,num);
    }
    preOrder(root);
    printf("\n");
    inOrder(root);
    printf("\n");
    postOrder(root);
}