ProgrammingHardProduct company OA · GATE CS
A binary tree is stored with child indices (−1 for none). What does h(0) return?
A binary tree is stored with child indices (−1 for none). What does h(0) return?
int L[] = {1, 3, -1, -1, -1}, R[] = {2, 4, -1, -1, -1};
int h(int n) {
if (n < 0) return 0;
int a = h(L[n]), b = h(R[n]);
return 1 + (a > b ? a : b);
}- A. 5
- B. 2
- C. 4
- D. 3
Correct answer: D. 3
Node 0 has children 1 and 2; node 1 has children 3 and 4. The longest root-to-leaf path is 0 → 1 → 3, which has 3 nodes, so the height (counting nodes) is 3. '5' is the number of nodes; '2' counts edges.
More C Programming questions
- With the tree below, what does in(0) print? c char key[] = "ABCDE"; int L[] = {1, 3, -1, -1, -1}, R[] = {2, 4, -1, -1, -1}; void in(int n)…
- What is wrong with this code? c char s = malloc(5); strcpy(s, "hello");
- With 4-byte int, what is printed? c int a[3] = {7, 8, 9}; int (p)[3] = &a; printf("%d %d %zu", (p)[1], p + 1, sizeof p);
- What is printed? c int c = 0, calls = 0; void comb(int start, int n, int k) { calls++; if (k == 0) { c++; return; } for (int i = start; i…
- What is printed? c int x = 3, y = 1; printf("%d %d", !x == y, !(x == y));
- For ints a and b with b != 0 and no overflow, which statement is always true in C99 and later?