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));
}- A. 4
- B. 2
- C. 8
- 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
- What is printed? Each struct has an int a and an int b. c #include <stdio.h> #include <stdlib.h> struct P { int a, b; }; int cmp(const…
- What is printed? c #include <stdio.h> int main(void) { unsigned x = 11, y = x & (x + 1); printf("%u", y); }
- What is printed? Assume int is 4 bytes. c #include <stdio.h> int main(void) { int (p)[3] = 0; printf("%zu", sizeof p); }
- What is printed? Assume the array has space for 5 characters. c #include <stdio.h> int main(void) { char s[5] = "abcd"; printf("%zu"…
- What is printed? c #include <stdio.h> int main(void) { printf("%d", 'A' == 65); }
- What is printed? c #include <stdio.h> int main(void) { unsigned x = 0; x--; printf("%u", x >> 31); }