GATE Computer ScienceVery hardGATE CS (PYQ pattern)
struct T { int k; struct T *l, *r; }; int f(struct T *n) { return n ? 1 + max(f(n->l), f(n->r)) : 0; } int g(struct T *n) { return n ? max(max(g(n->l), g(n->r)), f(n->l) + f(n->r) + 1) : 0; } int h(struct T *n) { return n ? h(n->l) + h(n->r) + (!n->l && !n->r) : 0; } The BST obtained by inserting 8, 4, 12, 2, 6, 10, 14, 1, 3, 5, 7 in this order is passed to each function. What is f + g + h (the sum of the three values at the root)?
- A. 15
- B. 16
- C. 18
- D. 20
Correct answer: B. 16
f is the number of levels (4); g is the longest path counted in nodes (diameter, 6); h counts leaves (6). Sum = 16.
Practise more Programming and Data Structures (GATE) questionsProgramming and Data Structures (GATE): notes and key points
More Programming and Data Structures (GATE) questions
- Keys 30, 10, 40, 5, 20, 35, 50, 15, 25, 45 are inserted in this order into an empty BST, and then the left and right pointers of every…
- A complete binary tree with 20 nodes is stored in A[1..20] with A[i] = i, the children of i at 2i and 2i + 1. The routine v(i) does: if i…
- int a[4][3] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; int (p)[3] = a; p += 2; int q = p + 1; printf("%d %d %d %d", ((p - 1) + 2), (p)[1]…
- char n[] = {"delhi", "mumbai", "chennai", "kolkata"}; char pp = n; ++pp; printf("%s ", pp + 2); printf("%c ", ++pp); printf("%s ", pp[-1]…
- char src[] = "gate0cse"; char dst[20]; char d = dst, s = src; int n = 0; while ((d++ = s++)) n++; printf("%d %zu %zu", n, sizeof(src)…
- What is the total number of characters written to the output by this program? (Assume arguments of the last printf are evaluated before…