import scala.collection.mutable
import scala.io.StdIn.{readInt, readLine}

case class Node(left: Option[Int], right: Option[Int])

object Main extends App {
  import Solver.solve

  val n     = readInt()
  val lines = Iterator.fill(n)(readLine())

  val (xs, ys, zs) = solve(lines, n)

  println("Preorder")
  println(xs.mkString(" ", " ", ""))
  println("Inorder")
  println(ys.mkString(" ", " ", ""))
  println("Postorder")
  println(zs.mkString(" ", " ", ""))
}

object Solver {
  def solve(
      lines: Iterator[String],
      n: Int
  ): (List[Int], List[Int], List[Int]) = {

    val (nodes, rootId) = {
      val nodes      = mutable.Map.empty[Int, Node]
      val allNodeIds = mutable.Set(0 until n: _*)

      for {
        line <- lines
        Array(id, l, r) = line.split(' ').map { _.toInt }
      } {
        val left = if (l != -1) {
          allNodeIds -= l
          Some(l)
        } else None

        val right = if (r != -1) {
          allNodeIds -= r
          Some(r)
        } else None

        nodes(id) = Node(left, right)
      }

      val rootId = allNodeIds.head
      (nodes.toMap, rootId)
    }

    def preOrder(start: Int): List[Int] = {
      def loop(id: Int, zs: List[Int]): List[Int] = {
        val Node(left, right) = nodes(id)

        val list1 = id :: zs
        val list2 = left.fold(list1) { loop(_, list1) }
        val list3 = right.fold(list2) { loop(_, list2) }
        list3
      }
      val xs = loop(start, Nil)
      xs.reverse
    }

    def inOrder(start: Int): List[Int] = {
      def loop(id: Int, zs: List[Int]): List[Int] = {
        val Node(left, right) = nodes(id)

        val list1 = left.fold(zs) { loop(_, zs) }
        val list2 = id :: list1
        val list3 = right.fold(list2) { loop(_, list2) }
        list3
      }
      val xs = loop(start, Nil)
      xs.reverse
    }

    def postOrder(start: Int): List[Int] = {
      def loop(id: Int, zs: List[Int]): List[Int] = {
        val Node(left, right) = nodes(id)

        val list1 = left.fold(zs) { loop(_, zs) }
        val list2 = right.fold(list1) { loop(_, list1) }
        val list3 = id :: list2
        list3
      }
      val xs = loop(start, Nil)
      xs.reverse
    }

    val xs = preOrder(rootId)
    val ys = inOrder(rootId)
    val zs = postOrder(rootId)

    (xs, ys, zs)
  }
}

