Data Structures & AlgorithmsHardGATE CS · Codeforces Div 2
Distinct words ['bb', 'c', 'b', 'ac', 'cab'] 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. 5
- B. 4
- C. 3
- D. 2
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
- Connected undirected graph on vertices A, B, C, D, E, F, G, H with edges A–D (1), A–H (1), B–D (1), B–E (1), B–F (1), B–H (1), C–F (1)…
- Connected undirected weighted graph on vertices A, B, C, D, E, F with edges A–C (5), A–F (7), B–E (5), C–E (9), C–F (20), D–E (20), D–F…
- Directed acyclic graph on vertices A, B, C, D, E, F, G with edges F→D, F→C, A→C, A→E, A→G, D→E, E→B. Among all topological orderings…
- Strings a = "ccca" and b = "cbca". The edit distance allows insert, delete and substitute, each costing 1 (the standard Levenshtein…
- Tree with vertices 0 to 8, root 0, edges 0–1, 1–2, 1–3, 1–4, 2–5, 2–6, 6–7, 5–8 (parent–child). Vertex weights (vertex:weight) 0:11, 1:10…
- Array [27, 21, 1, 3, 16, 28, 17] is turned into a min-heap in place with bottom-up heapify: for i from ⌊n/2⌋−1 down to 0, sift the element…