GATE Computer ScienceHardGATE CS

The function below is called as g(12, 5). def g(n, k): if k == 0 or k == n: return 1 return g(n-1, k-1) + g(n-1, k) How many calls to g are made in total (including the first)?

  1. A. 1583
  2. B. 1584
  3. C. 1575
  4. D. 792

Correct answer: A. 1583

g(n, k) returns C(n, k) by adding leaf values of 1, so the recursion tree has C(12, 5) = 792 leaves. In a full binary tree the number of internal nodes is leaves − 1, so total calls = 2·792 − 1 = 1583.

More Algorithms questions

Exams that ask this topic