Data Structures & AlgorithmsVery hardICPC regional · GATE CS

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

Correct answer: D. 8 8 5 9 8 8 9 7

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