LeetCode 208 - Implement Trie (Prefix Tree)
一、问题描述 D e s c r i p t i o n : Implement a trie with insert, search, and startsWith methods. N o t e : You may assume that all inputs are consist of lowercase letters a-z . 二、解题报告 什么是trie树,trie树有哪些应用,怎么实现trie树,请看《 Trie树的应用与实现 》。 直接上代码: class TrieNode { public: bool iskey; // 标记该节点是否代表关键字 TrieNode *children[26]; // 各个子节点 TrieNode() { iskey = false; for(int i=0; i<26; ++i) children[i] = NULL; } }; class Trie { public: Trie() { root = new TrieNode(); } // 插入一个单词到trie树中 void insert(string s) { TrieNode* node = root; for(int i=0; i<s.size(); ++i) { if(node->children[s[i]-'a'] == NULL) { node-