#include <cstddef>
#include <iostream>
using namespace std;
struct node {
    int data;
    node* leftChild;
    node* rightChild;
    node(int a) {
        data = a;
        leftChild = NULL;
        rightChild = NULL;
    }
};
node* insert(node* root, int a) {
    if (root == NULL) {
        return new node(a);
    }
    if (a < root->data) root->leftChild = insert(root->leftChild,  a);
    else if (a > root->data)root->rightChild = insert(root->rightChild,  a);
    return root;
}
void  preOder(node* root) {
    if (root == NULL) return;
    cout << root->data << ' ';
    preOder(root->leftChild);
    preOder(root->rightChild);
}
void  inOder(node* root) {
    if (root == NULL) return;

    inOder(root->leftChild);
    cout << root->data << ' ';
    inOder(root->rightChild);
}
void  postOder(node* root) {
    if (root == NULL) return;
    postOder(root->leftChild);
    postOder(root->rightChild);
    cout << root->data << ' ';
}
int main() {
    int n;
    while (cin >> n) { // 注意 while 处理多个 case

        node* root = NULL;
        while (n--) {
            int a;
            cin >> a;
            root = insert(root, a);
        }
        preOder(root);
        cout<<endl;
        inOder(root);
        cout<<endl;
        postOder(root);
        cout<<endl;
    }
}
// 64 位输出请用 printf("%lld")