原题链接
Implement a trie with insert, search, and startsWith methods.
Example:
Trie trie = new Trie();trie.insert("apple");trie.search("apple"); // returns truetrie.search("app"); // returns falsetrie.startsWith("app"); // returns truetrie.insert("app");trie.search("app"); // returns true
Note:
You may assume that all inputs are consist of lowercase letters a-z.
All inputs are guaranteed to be non-empty strings.

class Trie {private:bool isEnd;Trie* next[26];public:Trie() {isEnd = false;memset(next, 0, sizeof(next));}void insert(string word) {Trie* node = this;for (char c : word) {if (node->next[c-'a'] == NULL) {node->next[c-'a'] = new Trie();}node = node->next[c-'a'];}node->isEnd = true;}bool search(string word) {Trie* node = this;for (char c : word) {node = node->next[c - 'a'];if (node == NULL) {return false;}}return node->isEnd;}bool startsWith(string prefix) {Trie* node = this;for (char c : prefix) {node = node->next[c-'a'];if (node == NULL) {return false;}}return true;}};
