Data Structures & AlgorithmsHardGATE CS

What does f(3) print?

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

Correct answer: A. 31132211

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 31132211 (8 digits).

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic