Data Structures & AlgorithmsHardGATE CS

What does f(4) print?

What does f(4) print?
void f(int n) {
    if (n <= 0) return;
    printf("%d", n);
    f(n - 2);
    printf("%d", n);
    f(n - 1);
}
  1. A. 43211231142112
  2. B. 21124113211234
  3. C. 42211431132211
  4. D. 11223113411224

Correct answer: C. 42211431132211

Expand the recursion tree in order: print n, the whole of f(n−2), print n again, then the whole of f(n−1). The output is 42211431132211 (14 digits).

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic