[LeetCode] Implement Trie (Prefix Tree)

简介: You need to understand what a Trie is before writing the code :-) This link has a nice solution, whose code is rewritten below.

You need to understand what a Trie is before writing the code :-) This link has a nice solution, whose code is rewritten below.

 1 class TrieNode {
 2 public:
 3     bool isWord;
 4     TrieNode* children[26];
 5     // Initialize your data structure here.
 6     TrieNode() : isWord(false) {
 7         memset(children, NULL, sizeof(TrieNode*) * 26);
 8     }
 9 };
10 
11 class Trie {
12 public:
13     Trie() {
14         root = new TrieNode();
15     }
16 
17     // Inserts a word into the trie.
18     void insert(string word) {
19         TrieNode* run = root;
20         for (char c : word) {
21             if (!(run -> children[c - 'a']))
22                 run -> children[c - 'a'] = new TrieNode();
23             run = run -> children[c - 'a'];
24         }
25         run -> isWord = true;
26     }
27 
28     // Returns if the word is in the trie.
29     bool search(string word) {
30         TrieNode* p = query(word);
31         return p && p -> isWord;
32     }
33 
34     // Returns if there is any word in the trie
35     // that starts with the given prefix.
36     bool startsWith(string prefix) {
37         return query(prefix);
38     }
39 
40 private:
41     TrieNode* root;
42     TrieNode* query(string& s) {
43         TrieNode* run = root;
44         for (char c : s) {
45             if (!(run -> children[c - 'a'])) return NULL;
46             run = run -> children[c - 'a'];
47         }
48         return run;
49     }
50 };
51 
52 // Your Trie object will be instantiated and called as such:
53 // Trie trie;
54 // trie.insert("somestring");
55 // trie.search("key");

 

目录
相关文章
|
Java
Leetcode 114. Flatten Binary Tree to Linked List
思路也很简单,先把root的左子树(如有)变成单链表 leftlinkedlist,把root的右子树(如有)变成单链表 rightlinkedlist,再把root的右节点变成leftlikedlist,再把rightlinkedlist接到leftlinkedlist后面,代码如下。
203 1
Leetcode Minimum Depth of Binary Tree (面试题推荐)
计算树的最小深度 很简单的一道题,只需要遍历一次树,到叶子节点的时候计算一下深度和当前最小深度比较,保存最小值就行。 我在这用了一个全局变量 mindepth。总感觉我这代码写的不够简练,求更精简的方法。
222 0
Leetcode Binary Tree Postorder Traversal(面试题推荐)
非递后续归遍历二叉树,肯定得用到栈。先序遍历很好写,但后续遍历就不是那么容易了。 只需要设置个指针pre,指向最后输出的那个节点就行了,只要判断cur指针指向的是上次输出节点的父节点,且cur无其他未遍历的节点,这个时候就把cur节点输出即可,然后更改pre。原理是要遍历当前节点,其所有子节点都必须遍历完,因为肯定是先左后右,所以只需一个指针保持前一次输出的结果即可。
196 0
Leetcode 236. Lowest Common Ancestor of a Binary Tree
根据LCA的定义,二叉树中最小公共祖先就是两个节点p和q最近的共同祖先节点,LCA的定义没什么好解释的,主要是这道题的解法。
223 0
|
存储 自然语言处理 算法
【LeetCode 热题100】208:实现 Trie (前缀树)(详细解析)(Go语言版)
本文详细解析了力扣热题 208——实现 Trie(前缀树)。Trie 是一种高效的树形数据结构,用于存储和检索字符串集合。文章通过插入、查找和前缀匹配三个核心操作,结合 Go 语言实现代码,清晰展示了 Trie 的工作原理。时间复杂度为 O(m),空间复杂度也为 O(m),其中 m 为字符串长度。此外,还探讨了 Trie 的变种及应用场景,如自动补全和词典查找等。适合初学者深入了解 Trie 结构及其实际用途。
582 14
|
Go
golang力扣leetcode 208.实现Trie(前缀树)
golang力扣leetcode 208.实现Trie(前缀树)
201 0
|
存储 C++
leetcode-208:实现 Trie (前缀树/字典树)
leetcode-208:实现 Trie (前缀树/字典树)
188 0
|
存储 算法 vr&ar
☆打卡算法☆LeetCode 208. 实现 Trie (前缀树) 算法解析
☆打卡算法☆LeetCode 208. 实现 Trie (前缀树) 算法解析
Leetcode 623. Add One Row to Tree
题目很简单,在树的第d层加一层,值为v。递归增加一层就好了。代码如下
193 0
|
存储 Java
力扣208:实现 Trie (前缀树) (Java多种数据结构)
Trie(发音类似 "try")或者说 前缀树 是一种树形数据结构,用于高效地存储和检索字符串数据集中的键。这一数据结构有相当多的应用情景,例如自动补完和拼写检查。
493 0
力扣208:实现 Trie (前缀树) (Java多种数据结构)

热门文章

最新文章