#include <stdio.h>
#include <stdlib.h>

#define UND_NODE -1
#define UND_HEIGHT -1

typedef struct {
//    int id;
    int right;
    int left;
    int parent;
    int height;
} BinaryNode;

int nodeDepthFromId(BinaryNode nodes[], int length, int id) {
    int i = 0;
    while (nodes[id].parent != UND_NODE) {
        i++;
        id = nodes[id].parent;
    }
    return i;
}

int nodeSiblingFromId(BinaryNode nodes[], int length, int id) {
    if (nodes[id].parent == UND_NODE)
        return UND_NODE;
    
    BinaryNode parentNode = nodes[nodes[id].parent];
    
    if (parentNode.left == id && parentNode.right != UND_NODE)
        return parentNode.right;
    
    if (parentNode.right == id && parentNode.left != UND_NODE)
        return parentNode.left;
    
    return UND_NODE;
}

int constructHeights(BinaryNode nodes[], int length) {
    int i, parentId, heightCount;
    int rootId = 0; // assuming only single root
    
    for (i = 0; i < length; i++) {
        if (nodes[i].left == UND_NODE && nodes[i].right == UND_NODE) {
            nodes[i].height = 0;

            parentId = nodes[i].parent;
            heightCount = 1;
            while (1) {
                if (parentId == UND_NODE) {
                    rootId = parentId;
                    break;
                }
                if (nodes[parentId].height < heightCount)
                    nodes[parentId].height = heightCount;
                

                heightCount++;
                parentId = nodes[parentId].parent;
            }
        }
    }
    
    return rootId;
}

void printArray(int array[], int length) {
    for (int i = 0; i < length; i++) {
        if (i) printf(", ");
        printf("%d", array[i]);
    }
}


void preParse(BinaryNode nodes[], int length, int id) {
    if (id == UND_NODE)
        return;
    
    printf(" %d", id);
    preParse(nodes, length, nodes[id].left);
    preParse(nodes, length, nodes[id].right);
}

void inParse(BinaryNode nodes[], int length, int id) {
    if (id == UND_NODE)
        return;
    
    inParse(nodes, length, nodes[id].left);
    printf(" %d", id);
    inParse(nodes, length, nodes[id].right);
}

void postParse(BinaryNode nodes[], int length, int id) {
    if (id == UND_NODE)
        return;
    
    postParse(nodes, length, nodes[id].left);
    postParse(nodes, length, nodes[id].right);
    printf(" %d", id);
}


int main(int argc, char** argv) {
    int nNodes, rootId;
    scanf("%d", &nNodes);
    
    BinaryNode nodes[nNodes]; // Binary tree
    int i, j, nodeId, left, right;
    
    for (i = 0; i < nNodes; i++) {
        nodes[i].parent = nodes[i].left = nodes[i].right = UND_NODE;
        nodes[i].height = UND_HEIGHT;
    }
    
    for (i = 0; i < nNodes; i++) {
        scanf("%d", &(nodeId)); // id
        scanf("%d", &(left)); // 
        scanf("%d", &(right)); // 

        nodes[nodeId].left = left;
        nodes[nodeId].right = right;
        nodes[left].parent = nodes[right].parent = nodeId;
    }
    
    // get root
    for (i = 0; i < nNodes; i++)
        if (nodes[i].parent == UND_NODE) {
            rootId = i;
            break;
        }
    
//    // for height
//    rootId = constructHeights(nodes, nNodes);
    
    printf("Preorder\n");
    preParse(nodes, nNodes, rootId);
    printf("\n");
    printf("Inorder\n");
    inParse(nodes, nNodes, rootId);
    printf("\n");
    printf("Postorder\n");
    postParse(nodes, nNodes, rootId);
    printf("\n");
    

    return (EXIT_SUCCESS);
}




Measured memory_bytes: 2108