#include<stdio.h>
#include<stdlib.h>
typedef struct node{
    int data;
    struct node *left;
    struct node *right;
}Node;
Node *root=NULL;

Node *create(int val){
    if(val==0){
        return NULL;
    }
    Node *newnode=(Node*)malloc(sizeof(Node));
    newnode->data=val;
    newnode->left=newnode->right=NULL;
    return newnode;
}

Node *buildtree(int arr[],int n,int i){
    if(root==NULL){
        return;
    }
    Node *root=create(arr[i]);
    root->left=buildtree(arr,n,2*i+1);
    root->right=buildtree(arr,n,2*i+2);
    return root;
}

void preorder(){
    if(root==NULL){
        return;
    }
    printf("%d ",root->data);
    preorder(root->left);
    preorder(root->right);
}

void inorder(){
    if(root==NULL){
        return;
    }
    inorder(root->left);
    printf("%d ",root->data);
    inorder(root->right);
}

void postorder(){
    if(root==NULL){
        return;
    }
    postorder(root->left);
    postorder(root->right);
    printf("%d ",root->data);
}

int main(){
    int n,arr[n];
    scanf("%d",&n);
    for(int i=0;i<n;i++)
        scanf("%d",&arr[i]);
    Node *root=buildtree(arr,n,0);
    preorder(root);
    printf("\n");
    inorder(root);
    printf("\n");
    postorder(root);
    printf("\n");
}