GATE Computer ScienceHardGATE CS (PYQ pattern)

A complete binary tree (every level full except possibly the last, which is filled from the left) has exactly 300 leaves. Which node counts n are possible, and for the largest such n, how many leaves are NOT on the last level?

  1. A. only n = 599; 211 leaves
  2. B. n = 599 or 600; 212 leaves
  3. C. n = 599 or 600; 211 leaves
  4. D. n = 600 or 601; 211 leaves

Correct answer: C. n = 599 or 600; 211 leaves

In a complete tree the internal nodes are indices 1..floor(n/2), so leaves = n - floor(n/2) = ceil(n/2) = 300 gives n = 599 or 600. For n = 600, depths 0 to 8 hold 511 nodes and the last level holds 89; their parents are the first ceil(89/2) = 45 of the 256 nodes at depth 8, so 256 - 45 = 211 leaves sit at depth 8 (using floor(89/2) = 44 would give 212).

More Programming and Data Structures (GATE) questions

Exams that ask this topic