Data Structures & AlgorithmsHardCodeforces Div 2 · ICPC regional

Distinct words ['a', 'b', 'ba', 'bb', 'aaa', 'ac'] 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. 6
  2. B. 2
  3. C. 4
  4. D. 1

Correct answer: C. 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.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic