GATE Computer ScienceHardGATE CS

A tree has 16 nodes numbered 0 to 15 with the parent array parent[1..15] = 0, 0, 1, 1, 2, 2, 3, 3, 4, 5, 5, 6, 9, 9, 10 (node i has parent parent[i]; node 0 is the root). What is the diameter of the tree, i.e. the number of edges on the longest path between two nodes?

  1. A. 8
  2. B. 7
  3. C. 9
  4. D. 6

Correct answer: A. 8

Node 13 and node 14 hang below 9, which hangs below 4, 1 and 0; node 15 hangs below 10, 5 and 2. The longest path runs from 13 (or 14) up through 9, 4, 1, 0, 2, 5, 10 down to 15: 8 edges.

More Programming and Data Structures (GATE) questions

Exams that ask this topic