Implement Trie (Prefix Tree)

Easy
Concept
Code
Loading editor...
Tap Analyze to see visualization
Variables

Run code to see variables

Output

Console output will appear here

Press Space to start to step? all shortcuts

Solution Guide: Implement Trie (Prefix Tree)

Approach

Use nested child pointers under each node. Insert writes each character path and marks the terminal node. Search/startsWith walk the same path and check terminal or complete prefix existence.

Complexity Analysis

Time
O(L)
Space
O(total characters)

Pattern

Trie Construction and Lookup

Why It Works

Shared prefixes are stored once in the tree, and every query touches at most L nodes for an L-length word.

Updated Feb 2026