let nil = -1

type t = {
  mutable parent : int;
  mutable left : int;
  mutable right : int;
}

let make () = {
  parent = nil;
  left = nil;
  right = nil;
}

let search_root t n =
  let rec doit i =
    if i = n then nil
    else if t.(i).parent = nil then i
    else doit (i + 1)
  in doit 0

let preorder t root f =
  let rec doit i =
    f i;
    if t.(i).left <> nil then doit t.(i).left;
    if t.(i).right <> nil then doit t.(i).right
  in doit root

let inorder t root f =
  let rec doit i =
    if t.(i).left <> nil then doit t.(i).left;
    f i;
    if t.(i).right <> nil then doit t.(i).right
  in doit root

let postorder t root f =
  let rec doit i =
    if t.(i).left <> nil then doit t.(i).left;
    if t.(i).right <> nil then doit t.(i).right;
    f i
  in doit root

let () =
  let n = read_int () in
  let t = Array.init n (fun _ -> make ()) in
  let rec read i =
    if i < n then begin
      let (id, l, r) = Scanf.scanf "%d %d %d\n" (fun id l r -> (id, l, r)) in
      t.(id).left <- l;
      t.(id).right <- r;
      if l <> nil then t.(l).parent <- id;
      if r <> nil then t.(r).parent <- id;
      read (i + 1)
    end in
  read 0;
  let root = search_root t n in
  print_endline "Preorder";
  preorder t root (fun i -> Printf.printf " %d" i);
  print_newline ();
  print_endline "Inorder";
  inorder t root (fun i -> Printf.printf " %d" i);
  print_newline ();
  print_endline "Postorder";
  postorder t root (fun i -> Printf.printf " %d" i);
  print_newline ()