Data Structures & AlgorithmsHardICPC regional · GATE CS

A tree on vertices 1 to 10 has the edges 4–2, 7–1, 3–9, 2–9, 1–2, 8–4, 5–2, 6–8, 9–10. Compute its Prüfer code: repeat 8 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. 9 2 8 1 2 4 2 9
  2. B. 9 1 8 4 2 2 9 2
  3. C. 3 5 6 7 1 8 4 2
  4. D. 9 2 4 2 1 8 2 9

Correct answer: A. 9 2 8 1 2 4 2 9

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