data class Node(var parentNodeNum: Int = -1, var left: Pair<Int, Node>? = null, var right: Pair<Int, Node>? = null)

fun main(args: Array<String>) {
    val inputList = List(readLine()?.toInt() ?: return) {
        readLine()?.split(" ".toRegex())?.map(String::toInt) ?: listOf()
    }
    val nodeMap = mutableMapOf<Int, Node>()
    inputList.forEach { input ->
        val nodeNum = input[0]
        nodeMap.putIfAbsent(nodeNum, Node())
        nodeMap[nodeNum]?.let { node ->
            if (input[1] != -1) {
                val leftNum = input[1]
                nodeMap.putIfAbsent(leftNum, Node())
                nodeMap[leftNum]?.let { left ->
                    left.parentNodeNum = nodeNum
                    node.left = leftNum to left
                }
            }
            if (input[2] != -1) {
                val rightNum = input[2]
                nodeMap.putIfAbsent(rightNum, Node())
                nodeMap[rightNum]?.let { right ->
                    right.parentNodeNum = nodeNum
                    node.right = rightNum to right
                }
            }
        }
    }
    val tree = Tree(nodeMap)
    tree.preorderTreeWalk()
    tree.inorderTreeWalk()
    tree.postorderTreeWalk()
}

class Tree(private val nodeMap: Map<Int, Node>) {
    val root = nodeMap.toList().first { it.second.parentNodeNum == -1 }

    fun preorderTreeWalk() {
        println("Preorder")
        preorderTreeWalk(root)
        println()
    }

    private fun preorderTreeWalk(node: Pair<Int, Node>?) {
        if (node != null) {
            print(" ${node.first}")
            preorderTreeWalk(node.second.left)
            preorderTreeWalk(node.second.right)
        }
    }

    fun inorderTreeWalk() {
        println("Inorder")
        inorderTreeWalk(root)
        println()
    }

    private fun inorderTreeWalk(node: Pair<Int, Node>?) {
        if (node != null) {
            inorderTreeWalk(node.second.left)
            print(" ${node.first}")
            inorderTreeWalk(node.second.right)
        }
    }

    fun postorderTreeWalk() {
        println("Postorder")
        postorderTreeWalk(root)
        println()
    }

    private fun postorderTreeWalk(node: Pair<Int, Node>?) {
        if (node != null) {
            postorderTreeWalk(node.second.left)
            postorderTreeWalk(node.second.right)
            print(" ${node.first}")
        }
    }
}
