Data Structures & AlgorithmsHardGATE CS · ICPC regional
Distinct words ['ca', 'a', 'ba', 'abc', '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. 4
- B. 5
- 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
- Undirected weighted graph on vertices A, B, C, D, E with edges A–C (2), A–D (3), A–E (1), B–C (3), B–E (3), C–D (3), D–E (3). An edge is…
- A trie keeps a counter at each node, and every word of the list W = ['c', 'accb', 'bbcb', 'c', 'cacb', 'b', 'bccb', 'c'] is inserted once…
- Array A = [7, 11, 5, 8, 11, 8]. Over all non-empty contiguous subarrays, what is the maximum value of the XOR of the elements in the…
- Undirected graph on vertices A, B, C, D, E, F with edges A–C (1), A–D (1), A–E (1), B–D (1), B–F (1), C–D (1), C–F (1), D–E (1), D–F (1)…
- 2 × 3 sliding puzzle with tiles 1 to 5 and one blank (shown as ). Current board: 4 1 / 2 5 3. Goal board: 1 2 3 / 4 5 . A move slides a…
- Undirected weighted graph on vertices A, B, C, D, E with edges A–B (1), A–C (2), A–E (2), B–C (2), C–D (1), C–E (3). How many distinct…