#include<iostream>
#include<stack>
#include"Header.h"
using namespace std;
void insert(Node<int>* root, int value) {
if (root->val == value) {
}
else if (root->val > value) {
if (root->left == NULL) {
root->left = new Node<int>();
root->left->val = value;
root->left->right = root->left->left = NULL;
return;
}
insert(root->left, value);
}
else if (root->val < value) {
if (root->right == NULL) {
root->right = new Node<int>();
root->right->val = value;
root->right->right = root->right->left = NULL;
return;
}
insert(root->right, value);
}
}
int main() {
Node<int>* root;
Node<int>* tem;
Node<int>* pre = NULL;
stack<Node<int>*> sta;
int n;
int value;
while (cin >> n) {
cin >> value;
root = new Node<int>();
root->val = value;
root->right = root->left = NULL;
for (int i = 1; i < n; i++) {
cin >> value;
insert(root, value);
}
while (!sta.empty()) sta.pop();
sta.push(root);
pre = root;
while (!sta.empty()) {
tem = sta.top();
if (tem->left == pre || tem->right == pre || (!tem->left && !tem->right)) {
sta.pop();
cout << tem->val << " ";
pre = tem;
}
else {
if (tem->right) {
sta.push(tem->right);
}
if (tem->left) {
sta.push(tem->left);
}
}
}
}
return 0;
}