ProgrammingHardCompetitive coding (CP)

How many nodes are at depth 2 in a perfect binary tree of depth 3 (the root is depth 0)? The program counts them recursively.

How many nodes are at depth 2 in a perfect binary tree of depth 3 (the root is depth 0)? The program counts them recursively.
#include <stdio.h>
int count(int d, int target) {
    if (d == target) return 1;
    return 2 * count(d + 1, target);
}
int main(void) {
    printf("%d", count(0, 2));
}
  1. A. 4
  2. B. 2
  3. C. 8
  4. D. 3

Correct answer: A. 4

Each level doubles the number of nodes, so depth 2 has 2 * 2 = 4 nodes.

More C Programming questions

Exams that ask this topic