Data Structures & AlgorithmsVery hardICPC regional · GATE CS

A tree on vertices 1 to 12 has the edges 2–7, 12–2, 9–8, 3–12, 7–1, 11–10, 4–1, 10–6, 5–9, 6–5, 1–6. 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. 10 6 9 5 6 1 1 12 2 7
  2. B. 12 1 9 5 6 10 6 1 7 2
  3. C. 3 4 8 9 5 11 10 6 1 7
  4. D. 2 7 1 6 10 6 5 9 1 12

Correct answer: B. 12 1 9 5 6 10 6 1 7 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