ProgrammingVery hardCompetitive coding (CP)
A complete binary tree of 7 nodes is stored in an array with children of node i at 2i+1 and 2i+2. The values are v = {5, 3, 8, 1, 4, 9, 2}. How many root-to-leaf paths have a sum greater than 12? Compute with this program.
A complete binary tree of 7 nodes is stored in an array with children of node i at 2i+1 and 2i+2. The values are v = {5, 3, 8, 1, 4, 9, 2}. How many root-to-leaf paths have a sum greater than 12? Compute with this program.
#include <stdio.h>
int v[7] = {5, 3, 8, 1, 4, 9, 2};
int cnt(int i, int sum) {
if (i >= 7) return 0;
sum += v[i];
if (2 * i + 1 >= 7) return sum > 12;
return cnt(2 * i + 1, sum) + cnt(2 * i + 2, sum);
}
int main(void) {
printf("%d", cnt(0, 0));
}- A. 1
- B. 3
- C. 4
- D. 2
Correct answer: D. 2
The four root-to-leaf paths sum to 9, 12, 22 and 15. Only the last two exceed 12, so the answer is 2.
More C Programming questions
- What is printed? c #include <stdio.h> #include <stdlib.h> struct Pair { int a, b; }; int cmp(const void p, const void q) { const struct…
- What is printed? c #include <stdio.h> #include <stdlib.h> #include <string.h> int cmp(const void a, const void b) { const char x = (const…
- How many subarrays of {2, 1, 4, 3, 1, 2, 4} have a sum of exactly 7? Compute with this program. c #include <stdio.h> int main(void) { int…
- What is printed? c #include <stdio.h> int main(void) { int a[] = {1, 3, -1, -3, 5, 3, 6, 7}; int k = 3, dq[8], head = 0, tail = 0, total =…
- How many values x in 0..255 satisfy (x & (uint8t)-x) == x? Assume uint8t is 8 bits. c #include <stdint.h> #include <stdio.h> int…
- What is printed? c #include <stdio.h> int f(int n) { static int calls = 0; calls++; if (n <= 0) return calls; int r = f(n - 2); return r…