Back to problems Solve on LeetCode → Prev: Climbing Stairs →

Implement Trie (Prefix Tree)

26-Way Tree for Prefix Storage — O(m) per operation

LeetCode 208 • Medium • Tries

Implement Trie with insert, search, and startsWith. Each node has 26 children (a–z). end marks word boundaries. Input: insert("apple"), insert("app")  →  search("apple")=true, startsWith("ap")=true

InsertO(m)m = word length
SearchO(m)follow path
SpaceO(n·m)n words, avg len m
Step0/12
root (empty)
Ready
Press Play or Step to watch insert("apple") build the trie. Each character: idx = ord(c) - 97; create child if None; traverse.