module BinaryTree = struct
  let nil = -1

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

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

module T = BinaryTree

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