#include <bits/stdc++.h>
using namespace std;

typedef struct Node
{
    int parent, left, right;
} Node;

class BinTree
{
private:
    Node *nodes;
    int num;
    int root;

public:
    BinTree(int n)
    {
        num = n;
        root = -1;
        nodes = (Node *)calloc(n, sizeof(Node));

        for (int i = 0; i < n; i++)
            nodes[i].parent = -1;
    };

    void setNode(int n, int left, int right)
    {
        nodes[n].left = left;
        setParent(left, n);
        nodes[n].right = right;
        setParent(right, n);
    };

    void setParent(int n, int parent)
    {
        nodes[n].parent = parent;
    };

    int getParent(int n)
    {
        return nodes[n].parent;
    };

    int getSibling(int n)
    {
        int parent = getParent(n);
        if (parent == -1)
            return -1;
        if (nodes[parent].right == n)
            return nodes[parent].left;
        return nodes[parent].right;
    };

    int getDegree(int n)
    {
        return (nodes[n].right != -1) + (nodes[n].left != -1);
    };

    int getDepth(int n)
    {
        int depth = 0;
        while (nodes[n].parent != -1)
        {
            depth++;
            n = nodes[n].parent;
        }
        return depth;
    };

    int getHeight(int n)
    {
        if (n == -1)
            return 0;
        return max(getHeight(nodes[n].right) + (nodes[n].right != -1), getHeight(nodes[n].left) + (nodes[n].left != -1));
    };

    int getType(int n)
    {
        if (nodes[n].parent == -1)
            return 0;
        if (nodes[n].right != -1 || nodes[n].left != -1)
            return 1;
        return 2;
    };

    void printType(int n)
    {
        switch (getType(n))
        {
        case 0:
            printf("root");
            break;
        case 1:
            printf("internal node");
            break;
        case 2:
            printf("leaf");
            break;
        }
    };

    void printInfo(int n)
    {
        printf("node %d: parent = %d, sibling = %d, degree = %d, depth = %d, height = %d, ", n, getParent(n), getSibling(n), getDegree(n), getDepth(n), getHeight(n));
        printType(n);
        printf("\n");
    };

    void printAll()
    {
        for (int i = 0; i < num; i++)
            printInfo(i);
    };

    void preOrder(int n)
    {
        if (n == -1)
            return;
        printf(" %d", n);
        preOrder(nodes[n].left);
        preOrder(nodes[n].right);
    };

    void inOrder(int n)
    {
        if (n == -1)
            return;
        inOrder(nodes[n].left);
        printf(" %d", n);
        inOrder(nodes[n].right);
    };

    void postOrder(int n)
    {
        if (n == -1)
            return;
        postOrder(nodes[n].left);
        postOrder(nodes[n].right);
        printf(" %d", n);
    };

    int getRoot()
    {
        if (root != -1)
            return root;
        for (int i = 0; i < num; i++)
        {
            if (getType(i) == 0)
            {
                root = i;
                return i;
            }
        }

        return 0;
    };
};

int main()
{
    int n;
    cin >> n;
    BinTree bt(n);
    int id, left, right;
    for (int i = 0; i < n; i++)
    {
        cin >> id >> left >> right;
        bt.setNode(id, left, right);
    }
    cout << "Preorder" << endl;
    bt.preOrder(bt.getRoot());
    cout << endl;
    cout << "Inorder" << endl;
    bt.inOrder(bt.getRoot());
    cout << endl;
    cout << "Postorder" << endl;
    bt.postOrder(bt.getRoot());
    cout << endl;
}


Measured memory_bytes: 3244