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.

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

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic