208. Implement Trie (Prefix Tree)

题目208. Implement Trie (Prefix Tree)

Implement a trie with insert, search, and startsWith methods.
Note:
You may assume that all inputs are consist of lowercase letters a-z.
Subscribe to see which companies asked this question

class TrieNode {
    boolean isLeaf;
    Map content;
    // Initialize your data structure here.
    public TrieNode() {
        content = new HashMap();
    }
}

public class Trie {
    private TrieNode root;

    public Trie() {
        root = new TrieNode();
    }

    // Inserts a word into the trie.
    public void insert(String word) {
        if(word == null || word.length() == 0){
            return;
        }
        
        TrieNode node = root;
        TrieNode tempNode = null;;
        for(int i=0, len=word.length(); i

你可能感兴趣的:(208. Implement Trie (Prefix Tree))