Data Structures & AlgorithmsHardCodeforces Div 2 · ICPC regional
Distinct words ['a', 'b', 'ba', 'bb', 'aaa', 'ac'] are inserted into an empty trie (one node per character, shared prefixes share nodes, and a root). How many leaves does the trie have? A leaf is a node with no children, and the root counts only when it has no children.
- A. 6
- B. 2
- C. 4
- D. 1
Correct answer: C. 4
A leaf is the end of a word that no other word extends, and there are 4 such words. Counting every word, or words that are proper prefixes of another, is the usual mistake.
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
- Undirected graph on vertices A, B, C, D, E, F, G with edges A–E (1), A–G (1), B–C (1), B–E (1), B–F (1), C–F (1), C–G (1), D–E (1), D–G…
- Consider the recursive function def f(n): if n < 3: return 1; return f(n-1) + f(n-3). Evaluate f(11) and count every invocation of f…
- 4 × 5 grid, rows top to bottom ('#' = blocked, '.' = open): ..#.. / ..#.. / ..... / ...#.. Count the paths from top-left to bottom-right…
- Complete undirected graph on vertices A to F with symmetric edge weights given row by row (row i, column j = weight of edge between…
- On an 7 × 7 chessboard with rows and columns numbered 0 to 6, a knight starts at (0, 3) and must reach (5, 2) (as (row, column)). A knight…
- Distinct keys [27, 2, 19, 11, 24, 4] are inserted, in the order given by the sequence [24, 11, 4, 19, 2, 27], into an empty binary search…