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.

  1. A. 2
  2. B. 4
  3. C. 3
  4. 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.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic