#include<stdio.h>
#include<stdlib.h>
typedef struct node{
    int data;
    struct node *left,*right;
}Node;
Node *root=NULL;
typedef struct tree{
    int data;
        Node *arr[20];
}Node1;

Node1 *q;
int f=0,r=-1;
void en(){
    q->arr[++rear]=root;
}
Node *de(){
    retrun q->arr[f++];
}
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,num){
    if(root=NULL)
    return create(num);
    if(num<root->data)
    root->left=insert(root->left,num);
    if(num>root->data)
    root->right=insert(root->right,num);
    return root;
}

void level(Node *root){
    q=(Node1*)malloc(sizeof(Node1));
    en(root);
    //printf("%d\n",q->arr[0]->data);
    while(f<=r){
        Node *temp=de();
        q=(Node1*)malloc(sizeof(Node1));
        printf("%d ",temp->data);
        if(temp->left!=NULL)
        en(temp->left);
        if(temp->right!=NULL)
        en(temp->right);
    }
}
int main(){
    int n,i,num;
    scanf("%d",&n);
    if(n<=0){
        printf("Invalid input");
        return 0;
    }
    for(i=0;i<n;i++){
        scanf("%d",&num);
        root=insert(root,num);
    }
    level(root);
    return 0;
}