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);
}- A. 31132211
- B. 32112311
- C. 11321123
- 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).
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- All suffixes of the string 'mississ' are inserted into a character trie (a suffix trie). How many nodes does it have, including the root?
- Keys 14, 86, 32, 49, 38, 76, 17, 91, 35, 81, 16 are inserted in that order into an empty BST. Then 32 is deleted and after that 86 is…
- The weighted undirected graph A–B (2), A–E (1), B–C (6), B–D (1), B–E (2), C–D (6), D–E (3) has repeated weights. How many different…
- What is the edit distance (insert, delete, substitute; each cost 1) between SUBSEQUENT and PLATFORM?
- Dijkstra's algorithm runs from A on the directed graph A→B (4), A→F (8), B→C (1), B→D (10), C→A (3), C→D (8), C→E (6), C→F (6), E→B (1)…
- In the DAG with edges A→E, A→G, B→E, C→A, C→B, C→E, C→F, C→G, D→F, E→F, G→F, how many distinct directed paths are there from C to F, and…