Implement Trie (Prefix Tree)

easy

Build insert/search/startsWith on a trie of characters

Implement Trie (Prefix Tree)

Key Insight

Store each word as a path of characters; inserts and lookups follow the same walk.

Step 1Create the Root
No characters yet
Array
HashMap
Empty

Start with an empty root node.

1 / 3

Learn the Pattern

Practice the Code

Step-by-Step Walkthrough: Implement Trie (Prefix Tree)

Store each word as a path of characters; inserts and lookups follow the same walk.

  1. Create the Root

    Start with an empty root node.

  2. Insert Words

    Insert "cat" and "car" by walking each character path.

  3. Search and Prefix

    "search("cat")" requires full path + terminal node, "startsWith("ca")" only needs the path.