@@ -125,7 +125,7 @@ def search(self, word: str) -> bool:
125125 return False # 直接返回 False
126126 cur = cur.children[ch] # 令当前节点指向新建立的节点,然后继续查找下一个字符
127127
128- return cur is not None and cur.isEnd # 判断当前节点是否为空,并且是否有单词结束标记
128+ return cur.isEnd # 判断是否有单词结束标记
129129```
130130
131131#### 2.3.2 字典树的查找前缀操作
@@ -140,7 +140,7 @@ def startsWith(self, prefix: str) -> bool:
140140 if ch not in cur.children: # 如果当前节点的子节点中,不存在键为 ch 的节点
141141 return False # 直接返回 False
142142 cur = cur.children[ch] # 令当前节点指向新建立的节点,然后继续查找下一个字符
143- return cur is not None # 判断当前节点是否为空,不为空则查找成功
143+ return True # 查找成功
144144```
145145
146146## 3. 字典树的实现代码
@@ -175,7 +175,7 @@ class Trie: # 字典树
175175 return False # 直接返回 False
176176 cur = cur.children[ch] # 令当前节点指向新建立的节点,然后继续查找下一个字符
177177
178- return cur is not None and cur.isEnd # 判断当前节点是否为空,并且是否有单词结束标记
178+ return cur.isEnd # 判断是否有单词结束标记
179179
180180 # 查找字典树中是否存在一个前缀
181181 def startsWith (self , prefix : str ) -> bool :
@@ -184,7 +184,7 @@ class Trie: # 字典树
184184 if ch not in cur.children: # 如果当前节点的子节点中,不存在键为 ch 的节点
185185 return False # 直接返回 False
186186 cur = cur.children[ch] # 令当前节点指向新建立的节点,然后继续查找下一个字符
187- return cur is not None # 判断当前节点是否为空,不为空则查找成功
187+ return True # 查找成功
188188```
189189
190190## 4. 字典树的算法分析
0 commit comments