Data Structures & AlgorithmsHardICPC regional · GATE CS top band
Distinct words ['ba', 'c', 'b', 'ca', 'bc', 'aac'] 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. 6
- D. 3
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 simple graph on vertices A, B, C, D, E, F, G with edges A–D (1), B–C (1), B–E (1), C–D (1), C–E (1), D–E (1), D–G (1), E–G (1)…
- Directed acyclic graph on vertices A, B, C, D, E, F, G (edges go from earlier to later letters) with weights A→C (4), A→F (1), B→D (2)…
- Directed graph on vertices A, B, C, D, E, F with edges F→D, C→F, A→D, A→C, F→E, F→C, B→E (no weights). What is the number of vertices in…
- Undirected weighted graph on vertices A, B, C, D with edges A–B (3), A–C (2), A–D (1), B–C (1), B–D (2). How many distinct minimum…
- Weighted tree on vertices 0 to 8 with edges 0–1 (6), 1–2 (7), 0–3 (8), 0–4 (1), 0–5 (1), 5–6 (6), 2–7 (2), 4–8 (2). What is the largest…
- Array A = [25, 23, 7, 3, 1] (all values below 32). What is the maximum of A[i] XOR A[j] over all index pairs with 0 ≤ i < j < 5?