Data Structures & AlgorithmsHardICPC regional · GATE CS

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

Correct answer: B. 7 2 7 3 1 2 3

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