Question

Breadth First Search (BFS) is started on a binary tree beginning from the root vertex. There is a vertex tt at a distance four from the root. If tt is the nn-th vertex in this BFS traversal, then the maximum possible value of nn is ________.

Official answer and solution
Official answer
31
From
Official GATE 2016 answer key

Solution

Find the maximum number of nodes in a binary tree up to distance 4 from the root in level-order traversal.

In a binary tree, let the root be at distance 0 (level 0). A vertex tt at distance 4 is at level 4.

Breadth First Search (BFS) explores all vertices level by level:

  • All vertices at level 0 are visited first,
  • then all vertices at level 1,
  • then all vertices at level 2,
  • then all vertices at level 3,
  • and finally vertices at level 4.

To maximize the index nn at which vertex tt is visited, the binary tree must contain the maximum possible number of vertices at each level up to level 4, and tt must be the last vertex visited at level 4:

  • Level 0: at most 20=12^0 = 1 vertex
  • Level 1: at most 21=22^1 = 2 vertices
  • Level 2: at most 22=42^2 = 4 vertices
  • Level 3: at most 23=82^3 = 8 vertices
  • Level 4: at most 24=162^4 = 16 vertices

The maximum total number of vertices up to and including the last vertex of level 4 is:

nmax⁡=1+2+4+8+16=25−1=31n_{\max} = 1 + 2 + 4 + 8 + 16 = 2^5 - 1 = 31

Thus, the maximum possible value of nn is 31.

Final answer

31

Common trap

Adding only vertices up to level 3 (1515) or confusing distance with tree height (forgetting that distance 4 means 5 levels).

The answer is the one in the official GATE answer key. The solution is this site's own explanation.

Source

Paper
GATE 2016 Computer Science and Information Technology (CS) Session 2
Question
21 of 65, printed as CS Q.11
Section
Computer Science and Information Technology

Report an error in this question

GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.