Data Structures & AlgorithmsVery hardICPC regional · GATE CS

A tree on vertices 1 to 12 has the edges 2–9, 7–12, 1–7, 5–9, 11–8, 9–10, 12–2, 3–9, 6–7, 4–2, 8–9. Compute its Prüfer code: repeat 10 times, delete the smallest-labelled leaf and write down the label of its neighbour. Give the code as the written numbers in order.

  1. A. 7 9 2 9 7 12 9 8 9 2
  2. B. 8 9 9 7 9 2 9 2 12 7
  3. C. 1 3 4 5 6 7 10 11 8 9
  4. D. 2 9 8 9 12 7 9 2 9 7

Correct answer: A. 7 9 2 9 7 12 9 8 9 2

Each step removes the smallest current leaf and records its unique neighbour; the trap answers come from removing the largest leaf, recording the leaf instead, or reversing the list.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic