Title: 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, and a binary tree called its right subtree.

Your task is to write a program that performs tree walks (systematically traverses all nodes in a tree) based on the following algorithms:

1. Print the root, the left subtree, and the right subtree (preorder).
2. Print the left subtree, the root, and the right subtree (inorder).
3. Print the left subtree, the right subtree, and the root (postorder).

The given binary tree consists of n nodes, and every 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 lines, the information of each node is given in the format: id left right. "id" is the node ID, "left" is the ID of the left child, and "right" is the 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.

# Buggy version:
from sys import stdin

class Node(object):
    def __init__(self, parent=None, left=None, right=None):
        self.parent = parent
        self.left = left
        self.right = right

def print_nodes(nodes, n):
    A = []
    B = []
    C = []
    def walk_tree(nodes, u):
        if u == -1:
            return
        r = nodes[u].right
        l = nodes[u].left
        nonlocal A
        A.append(u)
        walk_tree(nodes, l)
        B.append(u)
        walk_tree(nodes, r)
        C.append(u)

    for i in range(n):
        if nodes[i].parent == None:
            walk_tree(nodes, i)
            print("Preorder", end="\n ")
            print(*A, sep=" ")
            print("Ineorder", end="\n ")
            print(*B, sep=" ")
            print("Postorder", end="\n ")
            print(*C, sep=" ")

def read_binary_tree(nodes, n):
    for _ in range(n):
        i = [int(i) for i in stdin.readline().strip().split()]
        nodes[i[0]].left = i[1]
        nodes[i[0]].right = i[2]
        if i[1] != -1:
            nodes[i[1]].parent = i[0]
        if i[2] != -1:
            nodes[i[2]].parent = i[0]

n = int(input())
nodes = [Node() for _ in range(n)]
read_binary_tree(nodes, n)
print_nodes(nodes, n)


# Fixed version:
from sys import stdin

class Node(object):
    def __init__(self, parent=None, left=None, right=None):
        self.parent = parent
        self.left = left
        self.right = right

def print_nodes(nodes, n):
    A = []
    B = []
    C = []
    def walk_tree(nodes, u):
        if u == -1:
            return
        r = nodes[u].right
        l = nodes[u].left
        A.append(u)
        walk_tree(nodes, l)
        B.append(u)
        walk_tree(nodes, r)
        C.append(u)

    for i in range(n):
        if nodes[i].parent == None:
            walk_tree(nodes, i)
            print("Preorder", end="\n ")
            print(*A, sep=" ")
            print("Inorder", end="\n ")
            print(*B, sep=" ")
            print("Postorder", end="\n ")
            print(*C, sep=" ")

def read_binary_tree(nodes, n):
    for _ in range(n):
        i = [int(i) for i in stdin.readline().strip().split()]
        nodes[i[0]].left = i[1]
        nodes[i[0]].right = i[2]
        if i[1] != -1:
            nodes[i[1]].parent = i[0]
        if i[2] != -1:
            nodes[i[2]].parent = i[0]

n = int(input())
nodes = [Node() for _ in range(n)]
read_binary_tree(nodes, n)
print_nodes(nodes, n)

# Buggy version:
def s(x):
    print('',x,end='')
def p(u):
    if u+1:s(u);p(R[u]);p(L[u])
def i(u):
    if u+1:i(R[u]);s(u);i(L[u])
def o(u):
    if u+1:o(R[u]);o(L[u]);s(u)
        
n = int(input())
R, L = [0]*n, [0]*n
for _ in range(n):
    a, b, c = map(int, input().split())
    R[a] = b
    L[a] = c
print(R)
print(L)
root = (set(range(n))-set(R)-set(L)).pop()
print('Preorder');p(root)
print('\nInorder');i(root)
print('\nPostorder');o(root)
print()
     


# Fixed version:
def s(x):
    print('',x,end='')
def p(u):
    if u+1:s(u);p(R[u]);p(L[u])
def i(u):
    if u+1:i(R[u]);s(u);i(L[u])
def o(u):
    if u+1:o(R[u]);o(L[u]);s(u)
        
n = int(input())
R, L = [0]*n, [0]*n
for _ in range(n):
    a, b, c = map(int, input().split())
    R[a] = b
    L[a] = c

root = (set(range(n))-set(R)-set(L)).pop()
print('Preorder');p(root)
print('\nInorder');i(root)
print('\nPostorder');o(root)
print()

# Buggy version:
class Node:
    def __init__(self, id):
        self.id = id
        self.parent = None
        self.left = None
        self.right = None

root = None
n = int(input())
node_list = []
for i in range(n):
    node_list.append(Node(i))

for i in range(n):
    id, left, right = list(map(int, input().split()))
    node = node_list[id]
    if left > -1:
        node_list[left].parent = node
        node.left = node_list[left]
    if right > -1:
        node_list[right].parent = node
        node.right = node_list[right]

root = None
for node in node_list:
    if node.parent is None:
        root = node
        break

def preorder(node):
    if node is None:
        return
    print(' ' + str(node.id), end='')
    preorder(node.left)
    preorder(node.right)

def inorder(node):
    if node is None:
        return
    
    inorder(node.left)
    print(' ' + str(node.id), end='')
    inorder(node.right)

def postorder(node):
    if node is None:
        return
    print(' ' + str(node.id), end='')
    postorder(node.left)
    postorder(node.right)

print('Preorder')
preorder(root)
print('')

print('Inorder')
inorder(root)
print('')

print('Postorder')
postorder(root)
print('')


# Fixed version:
class Node:
    def __init__(self, id):
        self.id = id
        self.parent = None
        self.left = None
        self.right = None

root = None
n = int(input())
node_list = []
for i in range(n):
    node_list.append(Node(i))

for i in range(n):
    id, left, right = list(map(int, input().split()))
    node = node_list[id]
    if left > -1:
        node_list[left].parent = node
        node.left = node_list[left]
    if right > -1:
        node_list[right].parent = node
        node.right = node_list[right]

root = None
for node in node_list:
    if node.parent is None:
        root = node
        break

def preorder(node):
    if node is None:
        return
    print(' ' + str(node.id), end='')
    preorder(node.left)
    preorder(node.right)

def inorder(node):
    if node is None:
        return
    inorder(node.left)
    print(' ' + str(node.id), end='')
    inorder(node.right)

def postorder(node):
    if node is None:
        return
    postorder(node.left)
    postorder(node.right)
    print(' ' + str(node.id), end='')

print('Preorder')
preorder(root)
print('')

print('Inorder')
inorder(root)
print('')

print('Postorder')
postorder(root)
print('')

# Buggy version:
from collections import namedtuple

class TreeWalk:
    def __init__(self, t):
        self.t = t

    def preParse(self, u):
        if u == -1:
            return 
        print(" ", str(u), end = "")
        self.preParse(self.t[u].l)
        self.preParse(self.t[u].r)

    def inParse(self, u):
        if u == -1:
            return 
        self.inParse(self.t[u].l)
        print(" ", str(u), end = "")
        self.inParse(self.t[u].r)

    def postParse(self, u):
        if u == -1:
            return 
        self.postParse(self.t[u].l)
        self.postParse(self.t[u].r)
        print(" ", str(u), end = "")
    
if __name__ == '__main__':
    n = int(input().rstrip())
    Node = namedtuple('Node', ['p', 'l', 'r'])
    t = [Node(-1, -1, -1)] * n
    root = -1
    for i in range(n):
        v, l, r = [int(i) for i in input().rstrip().split(" ")]
        t[v] = t[v]._replace(l = l, r = r)
        if l != -1:
            t[l] = t[l]._replace(p = v)
        if r != -1:
            t[r] = t[r]._replace(p = v)
    for i in range(n):
        if (t[i].p == -1):
            root = i

    x = TreeWalk(t)
    print("Preorder")
    x.preParse(root)
    print()
    print("Inorder")
    x.inParse(root)
    print()
    print("Postorder")
    x.postParse(root)

# Fixed version:
from collections import namedtuple

class TreeWalk:
    def __init__(self, t):
        self.t = t

    def preParse(self, u):
        if u == -1:
            return 
        print("", str(u), end = "")
        self.preParse(self.t[u].l)
        self.preParse(self.t[u].r)

    def inParse(self, u):
        if u == -1:
            return 
        self.inParse(self.t[u].l)
        print("", str(u), end = "")
        self.inParse(self.t[u].r)

    def postParse(self, u):
        if u == -1:
            return 
        self.postParse(self.t[u].l)
        self.postParse(self.t[u].r)
        print("", str(u), end = "")
    
if __name__ == '__main__':
    n = int(input().rstrip())
    Node = namedtuple('Node', ['p', 'l', 'r'])
    t = [Node(-1, -1, -1)] * n
    root = -1
    for i in range(n):
        v, l, r = [int(i) for i in input().rstrip().split(" ")]
        t[v] = t[v]._replace(l = l, r = r)
        if l != -1:
            t[l] = t[l]._replace(p = v)
        if r != -1:
            t[r] = t[r]._replace(p = v)
    for i in range(n):
        if (t[i].p == -1):
            root = i

    x = TreeWalk(t)
    print("Preorder")
    x.preParse(root)
    print()
    print("Inorder")
    x.inParse(root)
    print()
    print("Postorder")
    x.postParse(root)
    print()

# Buggy version:
n = int(input())
tree = [0] * n
root = set(range(n))
for i in range(n):
    node_id, left, right = map(int, input().split())
    tree[node_id] = (left, right)
    root -= {left, right}
root_node = root.pop()


def preorder(i):
    if i == -1:
        return
    (left, right) = tree[i]
    yield i
    yield from preorder(left)
    yield from preorder(right)


def inorder(i):
    if i == -1:
        return
    (left, right) = tree[i]
    yield from inorder(left)
    yield i
    yield from inorder(right)


def postorder(i):
    if i == -1:
        return
    (left, right) = tree[i]
    yield from postorder(left)
    yield from postorder(right)
    yield i

print("preorder\n ", end="")
print(*preorder(root_node))
print("inorder\n ", end="")
print(*inorder(root_node))
print("postorder\n ", end="")
print(*postorder(root_node))

# Fixed version:
n = int(input())
tree = [0] * n
root = set(range(n))
for i in range(n):
    node_id, left, right = map(int, input().split())
    tree[node_id] = (left, right)
    root -= {left, right}
root_node = root.pop()


def preorder(i):
    if i == -1:
        return
    (left, right) = tree[i]
    yield i
    yield from preorder(left)
    yield from preorder(right)


def inorder(i):
    if i == -1:
        return
    (left, right) = tree[i]
    yield from inorder(left)
    yield i
    yield from inorder(right)


def postorder(i):
    if i == -1:
        return
    (left, right) = tree[i]
    yield from postorder(left)
    yield from postorder(right)
    yield i

print("Preorder\n ", end="")
print(*preorder(root_node))
print("Inorder\n ", end="")
print(*inorder(root_node))
print("Postorder\n ", end="")
print(*postorder(root_node))

# Buggy version:
n = int(input())
class Node(object):
    def __init__(self, p, l, r):
        self.p = p
        self.l = l
        self.r = r

nodes = [Node(None, None, None) for i in range(n)]

for i in range(n):
    id, left, right = map(int, input().split())
    nodes[id].l = left
    nodes[id].r = right
    if left != -1:
        nodes[left].p = i
    if right != -1:
        nodes[right].p = i

def pre(node,path):
    path.append(node)
    if nodes[node].l != -1:
        pre(nodes[node].l,path)
    if nodes[node].r != -1:
        pre(nodes[node].r,path)

def ino(node,path):
    if nodes[node].l != -1:
        ino(nodes[node].l,path)
    path.append(node)
    if nodes[node].r != -1:
        ino(nodes[node].r,path)

def post(node,path):
    if nodes[node].l != -1:
        post(nodes[node].l,path)
    if nodes[node].r != -1:
        post(nodes[node].r,path)
    path.append(node)

for ind, n in enumerate(nodes):
    if n.p == None:
        root = ind
        break
print('Preorder')
l = []
pre(root, l)
print(' '.join(map(str, l)))
print('InOrder')
l = []
ino(root, l)
print(' '.join(map(str, l)))
print('Postorder')
l = []
post(root,l)
print(' '.join(map(str, l)))

# Fixed version:
n = int(input())
class Node(object):
    def __init__(self, p, l, r):
        self.p = p
        self.l = l
        self.r = r

nodes = [Node(None, None, None) for i in range(n)]

for i in range(n):
    id, left, right = map(int, input().split())
    nodes[id].l = left
    nodes[id].r = right
    if left != -1:
        nodes[left].p = i
    if right != -1:
        nodes[right].p = i

def pre(node,path):
    path.append(node)
    if nodes[node].l != -1:
        pre(nodes[node].l,path)
    if nodes[node].r != -1:
        pre(nodes[node].r,path)

def ino(node,path):
    if nodes[node].l != -1:
        ino(nodes[node].l,path)
    path.append(node)
    if nodes[node].r != -1:
        ino(nodes[node].r,path)

def post(node,path):
    if nodes[node].l != -1:
        post(nodes[node].l,path)
    if nodes[node].r != -1:
        post(nodes[node].r,path)
    path.append(node)

for ind, n in enumerate(nodes):
    if n.p == None:
        root = ind
        break
print('Preorder')
l = []
pre(root, l)
print(' ' +  ' '.join(map(str, l)))
print('Inorder')
l = []
ino(root, l)
print(' ' +  ' '.join(map(str, l)))
print('Postorder')
l = []
post(root,l)
print(' ' + ' '.join(map(str, l)))

# Buggy version:
N = int(input())
tree = [None for _ in range(N)]
root = set(range(N))

for i in range(N):
    i, l, r = map(int,input().split())
    tree[i] = (l, r)
    root -= {l, r}

root_node = root.pop()

def preorder(i):
    if i == -1:
        return
    (l, r) = tree[i]
    print(" {}".format(i), end = "")
    preorder(l)
    preorder(r)
    
def inorder(i):
    if i == -1:
        return
    (l, r) = tree[i]
    inorder(l)
    print(" {}".format(i), end = "")
    inorder(r)
    
def postorder(i):
    if i == -1:
        return
    (l, r) = tree[i]
    inorder(l)
    inorder(r)
    print(" {}".format(i), end = "")
    
print('Preorder')
preorder(root_node)
print()

print('Inorder')
inorder(root_node)
print()

print('Postorder')
postorder(root_node)
print()

# Fixed version:
N = int(input())
tree = [None for _ in range(N)]
root = set(range(N))

for i in range(N):
    i, l, r = map(int,input().split())
    tree[i] = (l, r)
    root -= {l, r}

root_node = root.pop()

def preorder(i):
    if i == -1:
        return
    (l, r) = tree[i]
    print(" {}".format(i), end = "")
    preorder(l)
    preorder(r)
    
def inorder(i):
    if i == -1:
        return
    (l, r) = tree[i]
    inorder(l)
    print(" {}".format(i), end = "")
    inorder(r)
    
def postorder(i):
    if i == -1:
        return
    (l, r) = tree[i]
    postorder(l)
    postorder(r)
    print(" {}".format(i), end = "")
    
print('Preorder')
preorder(root_node)
print()

print('Inorder')
inorder(root_node)
print()

print('Postorder')
postorder(root_node)
print()

# Buggy version:
class Node():
    def __init__(self, node_id, left, right):
        self.node_id    =   node_id
        self.left       =   left
        self.right      =   right

    def pre_order_search(self):
        l = self.left
        r = self.right
        print("",self.node_id, end="")
        if l != -1:
            tree[l].pre_order_search()
        if r != -1:
            tree[r].pre_order_search()

    def in_order_search(self):
        l = self.left
        r = self.right
        print(self.node_id, self.left, self.right)
        if l != -1:
            tree[l].in_order_search()
        print("",self.node_id, end="")
        if r != -1:
            tree[r].in_order_search()

    def post_order_search(self):
        l = self.left
        r = self.right
        if l != -1:
            tree[l].post_order_search()
        if r != -1:
            tree[r].post_order_search()
        print("",self.node_id, end="")

n = int(input())
tree = [None for i in range(n)]
root_set = set(range(n))

for i in range(n):
    node_id, left, right = map(int, input().split())
    tree[node_id] = Node(node_id, left, right)
    root_set -= set([left, right])
root = root_set.pop()

print("Preorder")
tree[root].pre_order_search()
print()

print("Inorder")
tree[root].in_order_search()
print()

print("Postorder")
tree[root].post_order_search()
print()

# Fixed version:
class Node():
    def __init__(self, node_id, left, right):
        self.node_id    =   node_id
        self.left       =   left
        self.right      =   right

    def pre_order_search(self):
        l = self.left
        r = self.right
        print("",self.node_id, end="")
        if l != -1:
            tree[l].pre_order_search()
        if r != -1:
            tree[r].pre_order_search()

    def in_order_search(self):
        l = self.left
        r = self.right
        if l != -1:
            tree[l].in_order_search()
        print("",self.node_id, end="")
        if r != -1:
            tree[r].in_order_search()

    def post_order_search(self):
        l = self.left
        r = self.right
        if l != -1:
            tree[l].post_order_search()
        if r != -1:
            tree[r].post_order_search()
        print("",self.node_id, end="")

n = int(input())
tree = [None for i in range(n)]
root_set = set(range(n))

for i in range(n):
    node_id, left, right = map(int, input().split())
    tree[node_id] = Node(node_id, left, right)
    root_set -= set([left, right])
root = root_set.pop()

print("Preorder")
tree[root].pre_order_search()
print("")

print("Inorder")
tree[root].in_order_search()
print("")

print("Postorder")
tree[root].post_order_search()
print("")

# Buggy version:
from collections import defaultdict
def preorder(here, conn, chain):
    if here == -1:
        return
    chain.append(here)
    if conn[here]:
        preorder(conn[here][0], conn, chain)
        preorder(conn[here][1], conn, chain)

def inorder(here, conn, chain):
    if here == -1:
        return
    if conn[here]:
        inorder(conn[here][0], conn, chain)
        chain.append(here)
        inorder(conn[here][1], conn, chain)

def postorder(here, conn, chain):
    if here == -1:
        return
    if conn[here]:
        postorder(conn[here][0], conn, chain)
        postorder(conn[here][1], conn, chain)
    chain.append(here)

query = int(input())
connect = defaultdict(list)
in_v = [0 for n in range(query + 1)]
for _ in range(query):
    here, left, right = (int(n) for n in input().split(" "))
    connect[here] = [left, right]
    in_v[left] += 1
    in_v[right] += 1
for i in range(query):
    if not in_v[i]:
        root = i
        break
preo = []
ino = []
posto = []
preorder(root, connect, preo)
inorder(root, connect, ino)
postorder(root, connect, posto)

print("Preorder")
print(" " + *preo)
print("Inorder")
print(" " + *ino)
print("Postorder")
print(" " + *posto)

# Fixed version:
from collections import defaultdict
def preorder(here, conn, chain):
    if here == -1:
        return
    chain.append(here)
    if conn[here]:
        preorder(conn[here][0], conn, chain)
        preorder(conn[here][1], conn, chain)

def inorder(here, conn, chain):
    if here == -1:
        return
    if conn[here]:
        inorder(conn[here][0], conn, chain)
        chain.append(here)
        inorder(conn[here][1], conn, chain)

def postorder(here, conn, chain):
    if here == -1:
        return
    if conn[here]:
        postorder(conn[here][0], conn, chain)
        postorder(conn[here][1], conn, chain)
    chain.append(here)

query = int(input())
connect = defaultdict(list)
in_v = [0 for n in range(query + 1)]
for _ in range(query):
    here, left, right = (int(n) for n in input().split(" "))
    connect[here] = [left, right]
    in_v[left] += 1
    in_v[right] += 1
for i in range(query):
    if not in_v[i]:
        root = i
        break
preo = []
ino = []
posto = []
preorder(root, connect, preo)
inorder(root, connect, ino)
postorder(root, connect, posto)

print("Preorder")
print("", end = " ")
print(*preo)
print("Inorder")
print("", end = " ")
print(*ino)
print("Postorder")
print("", end = " ")
print(*posto)

# Buggy version:
n = int(input())
root = set(range(n))
nodes = [0 for i in range(n)]
for i in range(n):
    node = list(map(int, input().split()))
    children = node[1:3]
    root -= set(children)
    nodes[node[0]] = children

def preorder(id):
    if(id == -1):
        return
    order.append(id)
    preorder(nodes[id][0])
    preorder(nodes[id][1])

def inorder(id):
    if(id == -1):
        return
    inorder(nodes[id][0])
    order.append(id)
    inorder(nodes[id][1])

def postorder(id):
    if(id == -1):
        return
    postorder(nodes[id][0])
    postorder(nodes[id][1])
    order.append(id)

order = []
preorder(list(root)[0])
print('Preorder')
print(*order)
order = []
inorder(list(root)[0])
print('Inorder')
print(*order)
order = []
postorder(list(root)[0])
print('Postorder')
print(*order)



# Fixed version:
n = int(input())
root = set(range(n))
nodes = [0 for i in range(n)]
for i in range(n):
    node = list(map(int, input().split()))
    children = node[1:3]
    root -= set(children)
    nodes[node[0]] = children

def preorder(id):
    if(id == -1):
        return
    order.append(id)
    preorder(nodes[id][0])
    preorder(nodes[id][1])

def inorder(id):
    if(id == -1):
        return
    inorder(nodes[id][0])
    order.append(id)
    inorder(nodes[id][1])

def postorder(id):
    if(id == -1):
        return
    postorder(nodes[id][0])
    postorder(nodes[id][1])
    order.append(id)

order = []
preorder(list(root)[0])
print('Preorder')
print(' ', end='')
print(*order)
order = []
inorder(list(root)[0])
print('Inorder')
print(' ', end='')
print(*order)
order = []
postorder(list(root)[0])
print('Postorder')
print(' ', end='')
print(*order)

# Buggy version:
def preorder(tree, now):
    if now == -1:
        return []
    else:
        left = tree[now][0]
        right = tree[now][1]
        return [now] + preorder(tree, left) + preorder(tree, right)


def inorder(tree, now):
    if now == -1:
        return []
    else:
        left = tree[now][0]
        right = tree[now][1]
        return inorder(tree, left) + [now] + inorder(tree, right)


def postorder(tree, now):
    if now == -1:
        return []
    else:
        left = tree[now][0]
        right = tree[now][1]
        return postorder(tree, left) + postorder(tree, right) + [now]


def print_list_split_whitespace(a):
    for x in a[:-1]:
        print(x, end=" ")
    print(a[-1])


n = int(input())
tree = [[] for _ in range(n)]
ps = [-1 for _ in range(n)]
ss = [-1 for _ in range(n)]
for _ in range(n):
    s = input().split()
    id = int(s[0])
    left = int(s[1])
    right = int(s[2])
    tree[id] = [left, right]
    if left >= 0:
        ps[left] = id
        ss[left] = right
    if right >= 0:
        ps[right] = id
        ss[right] = left

root = ps.index(-1)

print("Preorder")
pre_list = preorder(tree, root)
print_list_split_whitespace(pre_list)

print("Inorder")
in_list = inorder(tree, root)
print_list_split_whitespace(in_list)

print("Postorder")
post_list = postorder(tree, root)
print_list_split_whitespace(post_list)


# Fixed version:
def preorder(tree, now):
    if now == -1:
        return []
    else:
        left = tree[now][0]
        right = tree[now][1]
        return [now] + preorder(tree, left) + preorder(tree, right)


def inorder(tree, now):
    if now == -1:
        return []
    else:
        left = tree[now][0]
        right = tree[now][1]
        return inorder(tree, left) + [now] + inorder(tree, right)


def postorder(tree, now):
    if now == -1:
        return []
    else:
        left = tree[now][0]
        right = tree[now][1]
        return postorder(tree, left) + postorder(tree, right) + [now]


def print_list_split_whitespace(a):
    print(" ", end="")
    for x in a[:-1]:
        print(x, end=" ")
    print(a[-1])


n = int(input())
tree = [[] for _ in range(n)]
ps = [-1 for _ in range(n)]
ss = [-1 for _ in range(n)]
for _ in range(n):
    s = input().split()
    id = int(s[0])
    left = int(s[1])
    right = int(s[2])
    tree[id] = [left, right]
    if left >= 0:
        ps[left] = id
        ss[left] = right
    if right >= 0:
        ps[right] = id
        ss[right] = left

root = ps.index(-1)

print("Preorder")
pre_list = preorder(tree, root)
print_list_split_whitespace(pre_list)

print("Inorder")
in_list = inorder(tree, root)
print_list_split_whitespace(in_list)

print("Postorder")
post_list = postorder(tree, root)
print_list_split_whitespace(post_list)

# Buggy version:
n = int(input())
tree = [[-1, -1, True] for i in range(n)]
for i in range(n):
    id, left, right = map(int, input().split())
    if left != -1:
        tree[id][0] = left
        tree[left][2] = False
    if right != -1:
        tree[id][1] = right
        tree[right][2] = False

def preoder(id):
    if id == -1:
        return
    print(' ' + str(id), end='')
    preoder(tree[id][0])
    preoder(tree[id][1])

def inorder(id):
    if id == -1:
        return
    inorder(tree[id][0])
    print(' ' + str(id), end='')
    inorder(tree[id][1])

def postorder(id):
    if id == -1:
        return
    postorder(tree[id][0])
    postorder(tree[id][1])
    print(' ' + str(id), end='')

root = 0
for i, node in enumerate(tree):
    if node[2] == True:
        root = i

print('Preoder')
preoder(root)
print('')
print('Inorder')
inorder(root)
print('')
print('Postorder')
postorder(root)



# Fixed version:
n = int(input())
tree = [[-1, -1, True] for i in range(n)]
for i in range(n):
    id, left, right = map(int, input().split())
    if left != -1:
        tree[id][0] = left
        tree[left][2] = False
    if right != -1:
        tree[id][1] = right
        tree[right][2] = False

def preoder(id):
    if id == -1:
        return
    print(' ' + str(id), end='')
    preoder(tree[id][0])
    preoder(tree[id][1])

def inorder(id):
    if id == -1:
        return
    inorder(tree[id][0])
    print(' ' + str(id), end='')
    inorder(tree[id][1])

def postorder(id):
    if id == -1:
        return
    postorder(tree[id][0])
    postorder(tree[id][1])
    print(' ' + str(id), end='')

root = 0
for i, node in enumerate(tree):
    if node[2] == True:
        root = i

print('Preorder')
preoder(root)
print('')
print('Inorder')
inorder(root)
print('')
print('Postorder')
postorder(root)
print('')

# Buggy version:
#!/usr/bin/env python
from __future__ import division, print_function
from sys import stdin


class Node(object):
    def __init__(self):
        self.id = None
        self.parent = -1
        self.childs = None


def preorder_walk(data, index, out):
    node = data[index]
    out.append(node.id)
    for cidx in node.childs:
        if cidx >= 0:
            preorder_walk(data, cidx, out)


def inorder_walk(data, index, out):
    node = data[index]
    for cidx in node.childs:
        if cidx >= 0:
            inorder_walk(data, cidx, out)
            if node.id not in out:
                out.append(node.id)
    if node.id not in out:
        out.append(node.id)


def postorder_wark(data, index, out):
    node = data[index]
    for cidx in node.childs:
        if cidx >= 0:
            postorder_wark(data, cidx, out)
    out.append(node.id)


n = int(stdin.readline())
#assert 1 <= n <= 25
data = [Node() for _ in range(n)]
for _ in range(n):
    idx, left, right = (int(s) for s in stdin.readline().split())
    data[idx].id = idx
    data[idx].childs = [left, right]
    for c in data[idx].childs:
        data[c].parent = idx

for name, func in (('Preorder', preorder_walk), ('Inoder', inorder_walk),
                   ('Postorder', postorder_wark)):
    out = []
    func(data, 0, out)
    print(name)
    print(*out)

# Fixed version:
#!/usr/bin/env python
from __future__ import division, print_function
from sys import stdin


class Node(object):
    def __init__(self):
        self.id = None
        self.parent = -1
        self.childs = None


def preorder_walk(data, index, out):
    node = data[index]
    out.append(node.id)
    for cidx in node.childs:
        if cidx >= 0:
            preorder_walk(data, cidx, out)


def inorder_walk(data, index, out):
    cid = data[index].childs[0]
    if cid >= 0:
        inorder_walk(data, cid, out)
    out.append(data[index].id)
    cid = data[index].childs[1]
    if cid >= 0:
        inorder_walk(data, cid, out)


def postorder_wark(data, index, out):
    node = data[index]
    for cidx in node.childs:
        if cidx >= 0:
            postorder_wark(data, cidx, out)
    out.append(node.id)


n = int(stdin.readline())
#assert 1 <= n <= 25
data = [Node() for _ in range(n)]
for _ in range(n):
    idx, left, right = (int(s) for s in stdin.readline().split())
    data[idx].id = idx
    data[idx].childs = [left, right]
    for c in data[idx].childs:
        if c >= 0:
            data[c].parent = idx

start = None
for node in data:
    if node.parent == -1:
        start = node.id
        break

for name, func in (('Preorder\n', preorder_walk),
                   ('Inorder\n', inorder_walk),
                   ('Postorder\n', postorder_wark)):
    out = []
    func(data, start, out)
    print(name, *out)

# Buggy version:
def s(x):print(_,x,end='')
def p(u):
 if u+1:s(u);p(T[u]);p(T[u+N])
def i(u):
 if u+1:i(T[u]);s(u);i(T[u+N])
def o(u):
 if u+1:o(T[u]);o(T[u+N]);s(u)
N=int(input())
T=[0,0]*N
for _ in[0]*N:
 a,b,c=map(int,input().split())
 T[a],T[a+N]=b,c
r=(set(range(N))-set(T)).pop()
print('Preorder');p(r)
print('\nInorder');i(r)
print('\nPostorder');o(r)
print()


# Fixed version:
def s(x): print('', x, end = '')

def p(u):
    if u + 1: s(u); p(T[u]); p(T[u+N])
    
def i(u):
    if u + 1: i(T[u]); s(u); i(T[u+N])
    
def o(u):
    if u + 1: o(T[u]); o(T[u + N]); s(u)

N = int(input())

T = [0, 0] * N

for _ in[0] * N:
    a, b, c = map(int, input().split())
    T[a], T[a + N] = b, c

r = (set(range(N)) - set(T)).pop()

print('Preorder'); p(r)
print('\nInorder'); i(r)
print('\nPostorder'); o(r)
print()