GATE Computer ScienceVery hardGATE CS (PYQ pattern)

The number of nodes of a complete binary tree is found recursively: compute the leftmost-path height and the rightmost-path height of the current subtree; if they are equal the subtree is perfect and has 2^(h+1) - 1 nodes, else return 1 + count(left) + count(right). For a complete tree with 1000 nodes, how many calls to count are made (the first call included; a call on a null child counts)?

  1. A. 19
  2. B. 17
  3. C. 22
  4. D. 21

Correct answer: A. 19

Only the spine of non-perfect subtrees is expanded; each such node makes two child calls, one of which is perfect (returns immediately). The total is 19 calls.

More Programming and Data Structures (GATE) questions

Exams that ask this topic