Data Structures & AlgorithmsHardICPC regional · GATE CS

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

Correct answer: B. 4 9 3 5 6 5 4

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