Question
Breadth First Search (BFS) is started on a binary tree beginning from the root vertex. There is a vertex at a distance four from the root. If is the -th vertex in this BFS traversal, then the maximum possible value of 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 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 at which vertex is visited, the binary tree must contain the maximum possible number of vertices at each level up to level 4, and must be the last vertex visited at level 4:
- Level 0: at most vertex
- Level 1: at most vertices
- Level 2: at most vertices
- Level 3: at most vertices
- Level 4: at most vertices
The maximum total number of vertices up to and including the last vertex of level 4 is:
Thus, the maximum possible value of is 31.
31
Adding only vertices up to level 3 () 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.