Data Structures & AlgorithmsHardCodeforces Div 2 · ICPC regional
Distinct words ['aa', 'ab', 'ca', 'c', 'bb'] 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. 4
- B. 2
- C. 1
- D. 5
Correct answer: A. 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 weighted graph on vertices A, B, C, D, E with edges A–B (1), A–C (1), A–E (1), B–C (2), B–D (3), C–E (1), D–E (3). How many…
- Directed weighted graph on vertices A, B, C, D, E with edges (u→v, w): C→D (8); E→C (4); D→C (8); C→E (6); D→A (4); B→E (5). Let d(x, y)…
- Weighted tree on vertices 0 to 5 with edges 0–1 (9), 0–2 (8), 1–3 (4), 3–4 (4), 1–5 (3). What is the largest possible sum of edge weights…
- A binary tree with distinct letters has preorder DFEACB and inorder FDACEB. What is its postorder traversal (left, right, root), written…
- 5 × 5 grid, rows top to bottom ('#' = wall, '.' = open): ..... / ###.. / ....# / ...## / ##.#.. Start at top-left, target bottom-right…
- Points A(5,2), B(2,4), C(6,0), D(0,3), E(8,1) in the plane. The cost of joining two points is the Manhattan distance |x1−x2| + |y1−y2|…