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)?

  1. A. 15
  2. B. 16
  3. C. 18
  4. 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.

More Programming and Data Structures (GATE) questions

Exams that ask this topic