Data Structures & AlgorithmsHardICPC regional · GATE CS

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

Correct answer: A. 8 8 9 9 7 6 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