Data Structures & AlgorithmsHardCodeforces Div 2 · ICPC regional
Distinct words ['a', 'cbb', 'c', 'b'] 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. 2
- B. 4
- C. 3
- D. 1
Correct answer: C. 3
A leaf is the end of a word that no other word extends, and there are 3 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
- Directed weighted graph on vertices A, B, C, D, E with edges listed in this exact order: A→B (6); E→A (4); C→D (5); B→E (5); B→C (4); A→D…
- Distinct words ['ca', 'b', 'bac', 'a', 'c', 'bbbb'] are inserted into an empty trie (one node per character, shared prefixes sharing…
- A chain of 6 matrices M1 to M6 has dimensions given by the sequence p = [16, 18, 7, 19, 14, 15, 7], where Mi has size p[i−1] × p[i]…
- Connected undirected weighted graph on vertices A, B, C, D, E with edges A–B (12), A–C (16), A–D (15), B–C (2), B–D (20), B–E (17), C–E…
- Directed weighted graph on vertices A, B, C, D, E with edges (u→v, w): E→B (1); A→E (3); C→E (3); C→B (8); D→C (7); D→A (9); C→D (5); D→E…
- Bipartite graph with left side {A, B, C}, right side {D, E, F, G} and edges B–D, B–G, C–D, C–E, C–F. What is the size of a maximum…