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))
  tree(i) = (l, r)
  val root: Int =
    (1 until n).foldLeft(i) { (acc, _) =>
      val Seq(i,l,r): Seq[Int] = io.StdIn.readLine().split(" ").map(_.toInt)
      tree(i) = (l, r)
      if (acc == l || acc == r) i else acc
  }
  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))

  println(s"Preorder\n ${preorder(root).mkString(" ")}\nInorder\n ${inorder(root).mkString(" ")}\nPostorder\n ${postorder(root).mkString(" ")}")
}
