Data Structures & AlgorithmsModerateTCS NQT · GATE CS

Problem: A complete binary tree has all levels full with depths 0 to 4 (root at depth 0). How many nodes does it have in total?

  1. A. 16
  2. B. 63
  3. C. 32
  4. D. 31

Correct answer: D. 31

Levels 0..4 hold 1 + 2 + 4 + 8 + 16 = 2⁵ − 1 = 31 nodes. 16 counts only the bottom level, 63 adds a depth-5 level, and 2⁵ = 32 forgets the − 1.

More Coding Interview Problems questions

Exams that ask this topic