GATE Computer ScienceVery hardGATE CS (PYQ pattern)

int range(Node *n, int lo, int hi) { if (!n) return 0; int c = (lo <= n->k && n->k <= hi); if (lo < n->k) c += range(n->l, lo, hi); if (n->k < hi) c += range(n->r, lo, hi); return c; } The BST is built by inserting 50, 30, 70, 20, 40, 60, 80, 10, 25, 35, 45, 55, 65, 75, 85, 5, 22, 33, 38. For range(root, 24, 52), what is the returned value and how many nodes does the routine visit (calls with a non-NULL node)?

  1. A. 8 and 16
  2. B. 9 and 13
  3. C. 8 and 13
  4. D. 8 and 19

Correct answer: C. 8 and 13

The keys in [24, 52] are those listed in the result (8 of them). Subtrees entirely outside the range are skipped, so only 13 of the 19 nodes are visited.

More Programming and Data Structures (GATE) questions

Exams that ask this topic