object Main extends App {
  val n: Int = io.StdIn.readInt()
  val Seq(i, l, r): Seq[Int] = io.StdIn.readLine().split(" ").map(_.toInt)
  val tree: Array[(Int, Int)] = Array.fill(n)((-1, -1))
  val parent: Array[Option[Int]] = Array.fill(n)(None)
  tree(i) = (l, r)
  for (_ <- 1 until n) {
    val Seq(i,l,r): Seq[Int] = io.StdIn.readLine().split(" ").map(_.toInt)
    tree(i) = (l, r)
    if (l != -1) parent(l) = Some(i)
    if (r != -1) parent(r) = Some(i)
  }

  def find_root(c: Int): Int = parent(c) match {case None => c ; case Some(p) => find_root(p)}
  def order(c: Int)(f: (Int, List[Int], List[Int]) => List[Int]): List[Int] = {
    if (c == -1) Nil
    else {
      val (l, r) = tree(c)
      f(c, order(l)(f), order(r)(f))
    }
  }
  def preorder(rn: Int): List[Int] = order(rn)((i, l, r) => i :: (l ::: r))
  def inorder(rn: Int): List[Int] = order(rn)((i, l, r) => l ::: (i :: r))
  def postorder(rn: Int): List[Int] = order(rn)((i, l, r) => l ::: r ::: List(i))

  val root: Int = find_root(i)
  println(s"Preorder\n ${preorder(root).mkString(" ")}\nInorder\n ${inorder(root).mkString(" ")}\nPostorder\n ${postorder(root).mkString(" ")}")
}
