#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(1*sizeof(node));
    newnode->data=num;
    newnode->left=NULL;
    newnode->right=NULL;
    return newnode;
}
struct node* buildtree(int arr[],int n,int index){
    if(index>=n || arr[index]==0)
        return NULL;
    struct node* root=node(arr[index]);
    root->left=buildtree(arr,n,2*index+1);
    root->right=buildtree(arr,n,2*index+2);
    return root;
}
void preorder(struct node* root){
    if(root==NULL)return;
    printf("%d ",root->data);
    preorder(root->left);
    preorder(root->right);
}
void inorder(struct node* root){
    if(root==NULL)return;
    inorder(root->left);
    printf("%d ",root->data);
    inorder(root->right);
}
void postorder(struct node* root){
    if(root==NULL)return;
    postorder(root->left);
    postorder(root->right);
    printf("%d ",root->data);
}
int main(){
    int n;
    scanf("%d",&n);
    int arr[n];
    for(int i=0;i<n;i++)
        scanf("%d",&arr[i]);
    struct node* root=buildtree(arr,n,0);
    preorder(root);
    printf("\n");
    inorder(root);
    printf("\n");
    postorder(root);
    printf("\n");
    return 0;
}