Problem:
Tree Walk

Binary trees are defined recursively. A binary tree T is a structure defined on a finite set of nodes that either

contains no nodes, or

is composed of three disjoint sets of nodes:

- a root node.

- a binary tree called its left subtree.

- a binary tree called its right subtree.

Your task is to write a program which perform tree walks (systematically traverse all nodes in a tree) based on the following algorithms:

Print the root, the left subtree and right subtree (preorder).

Print the left subtree, the root and right subtree (inorder).

Print the left subtree, right subtree and the root (postorder).

Here, the given binary tree consists of n nodes and evey node has a unique ID from 0 to n-1.

Input

The first line of the input includes an integer n, the number of nodes of the tree.

In the next n linen, the information of each node is given in the following format:

id left right

id is the node ID, left is ID of the left child and right is ID of the right child. If the node does not have the left (right) child, the left(right) is indicated by -1

Output

In the 1st line, print "Preorder", and in the 2nd line print a list of node IDs obtained by the preorder tree walk.

In the 3rd line, print "Inorder", and in the 4th line print a list of node IDs obtained by the inorder tree walk.

In the 5th line, print "Postorder", and in the 6th line print a list of node IDs obtained by the postorder tree walk.

Print a space character before each node ID.

Constraints

1 ≤ n ≤ 25

Sample Input 1

9
0 1 4
1 2 3
2 -1 -1
3 -1 -1
4 5 8
5 6 7
6 -1 -1
7 -1 -1
8 -1 -1

Sample Output 1

Preorder
0 1 2 3 4 5 6 7 8
Inorder
2 1 3 0 6 5 7 4 8
Postorder
2 3 1 6 7 5 8 4 0

Reference

Introduction to Algorithms, Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. The MIT Press.

Solution in JAVA (judge verdict: WA: Presentation Error)

import java.util.*;
import java.io.*;

public class Main {
    
    public static void main(String[] args) {
        Main main = new Main();
        main.solv();
    }
    
    private void solv() {
        Scanner input = new Scanner(System.in);
        
        int n = input.nextInt();
        Node[] nodes = new Node[n];
        for (int i = 0; i < n; i++) {
            nodes[i] = new Node(i);
        }
        for (int i = 0; i < n; i++) {
            int id = input.nextInt();
            int left = input.nextInt();
            int right = input.nextInt();
            nodes[id].setLeft(left);
            nodes[id].setRight(right);
            if (left != -1) {
                nodes[left].setParent(id);
                nodes[left].setSibling(right);
            }
            if (right != -1) {
                nodes[right].setParent(id);
                nodes[right].setSibling(left);
            }
        }
        int root = -1;
        for (Node node : nodes) {
            if (node.getParent() == -1) {
                root = node.getId();
                updateNodes(node, nodes);
                break;
            }
        }
        System.out.println("Preorder");
        System.out.println(preorder(nodes[root], nodes).trim());
        System.out.println("Inorder");
        System.out.println(inorder(nodes[root], nodes).trim());
        System.out.println("Postorder");
        System.out.println(postorder(nodes[root], nodes).trim());
    }
    
    private String preorder(Node node, Node[] nodes) {
        StringBuilder sb = new StringBuilder();
        sb.append(node.getId()).append(' ');
        if (node.getLeft() != -1) sb.append(preorder(nodes[node.getLeft()], nodes));
        if (node.getRight() != -1) sb.append(preorder(nodes[node.getRight()], nodes));
        return sb.toString();
    }
    private String inorder(Node node, Node[] nodes) {
        StringBuilder sb = new StringBuilder();
        if (node.getLeft() != -1) sb.append(inorder(nodes[node.getLeft()], nodes));
        sb.append(node.getId()).append(' ');
        if (node.getRight() != -1) sb.append(inorder(nodes[node.getRight()], nodes));
        return sb.toString();
    }
    private String postorder(Node node, Node[] nodes) {
        StringBuilder sb = new StringBuilder();
        if (node.getLeft() != -1) sb.append(postorder(nodes[node.getLeft()], nodes));
        if (node.getRight() != -1) sb.append(postorder(nodes[node.getRight()], nodes));
        sb.append(node.getId()).append(' ');
        return sb.toString();
    }
    
    private int updateNodes(Node node, Node[] nodes) {
        int l = 0;
        if (node.hasLeft()) {
            nodes[node.getLeft()].setDepth(node.getDepth() + 1);
            l = updateNodes(nodes[node.getLeft()], nodes);
        }
        int r = 0;
        if (node.hasRight()) {
            nodes[node.getRight()].setDepth(node.getDepth() + 1);
            r = updateNodes(nodes[node.getRight()], nodes);
        }
        node.setHeight(Math.max(l,r));
        return node.getHeight()+1;
    }
    
    private static class Node {
        int id;
        int sibling = -1;
        int left = -1;
        int right = -1;
        int parent = -1;
        int depth = 0;
        int height = 0;
        Node(int id) {
            this.id = id;
        }
        int getId() {
            return id;
        }
        int getSibling() {
            return sibling;
        }
        void setSibling(int sibling) {
            this.sibling = sibling;
        }
        int getLeft() {
            return left;
        }
        void setLeft(int left) {
            this.left = left;
        }
        boolean hasLeft() {
            return left != -1;
        }
        int getRight() {
            return right;
        }
        void setRight(int right) {
            this.right = right;
        }
        boolean hasRight() {
            return right != -1;
        }
        int getParent() {
            return parent;
        }
        void setParent(int parent) {
            this.parent = parent;
        }
        int getDepth() {
            return depth;
        }
        void setDepth(int depth) {
            this.depth = depth;
        }
        int getHeight() {
            return height;
        }
        void setHeight(int height) {
            this.height = height;
        }
        int getDegree() {
            return (hasLeft() ? 1 : 0) + (hasRight() ? 1 : 0);
        }
        String getType() {
            if (depth == 0) return "root";
            if (getDegree() == 0) return "leaf";
            return "internal node";
        }
        private static String TEMPLATE = "node %d: parent = %d, sibling = %d, degree = %d, depth = %d, height = %d, %s";
        @Override public String toString() {
            StringBuilder sb = new StringBuilder();
            int degree = getDegree();
            sb.append(String.format(TEMPLATE, id, parent, sibling, degree, depth, height, getType()));
            return sb.toString();
        }
    }
    
    private void print(Object[] xs) {
        StringBuilder sb = new StringBuilder();
        for (Object x : xs) sb.append(x.toString()).append(' ');
        System.out.println(sb.toString().trim());
    }
    
    private void print(int[] xs) {
        StringBuilder sb = new StringBuilder();
        for (int x : xs) sb.append(x).append(' ');
        System.out.println(sb.toString().trim());
    }
    
    private static class Scanner {
        BufferedReader r;
        StreamTokenizer st;
        
        public Scanner(InputStream in) {
            r = new BufferedReader(new InputStreamReader(in));
            st = new StreamTokenizer(r);
        }
        
        public String next() {
            nextToken();
            return st.sval;
        }
        
        public Integer nextInt() {
            nextToken();
            return (int)st.nval;
        }
        
        private int nextToken() {
            int token = st.TT_EOF;
            try {
                token = st.nextToken();
            } catch (IOException e) {
            }
            return token;
        }
    }
}