25. Trie

Prefix trees for words, autocomplete and XOR tricks.

🎬 Episode 97: video coming soon
Your progress0 of 8
0easy
6medium
2hard

Understand the topic

A trie (prefix tree) stores words letter by letter. Words that share a beginning share a path, so "car", "cart" and "care" all use the c β†’ a β†’ r nodes. Each node has up to 26 children and a flag that says "a word ends here".

Lookups take O(word length), however many words are stored. Tries power autocomplete and prefix search. A binary trie (two children per node, one per bit) also solves maximum-XOR problems.

#ProblemPatternLevel
1 Implement Trie (Prefix Tree) #208 Trie basics Medium LeetCode β†—
2 Design Add and Search Words Data Structure #211 Trie + DFS for wildcards Medium LeetCode β†—
3 Search Suggestions System #1268 Trie with top-3 lists Medium LeetCode β†—
4 Replace Words #648 Shortest root in a trie Medium LeetCode β†—
5 Longest Word in Dictionary #720 Trie walk through complete words Medium LeetCode β†—
6 Maximum XOR of Two Numbers in an Array #421 Binary trie Medium LeetCode β†—
7 Word Search II #212 Trie-guided grid DFS Hard LeetCode β†—
8 Maximum XOR With an Element From Array #1707 Offline queries + binary trie Hard LeetCode β†—

Work top to bottom β€” each list goes from easy to hard. Try a problem for at least 15 minutes before opening a hint. Ticks are saved in your browser.