class trie_node:
def __init__(self):
self.children1 = dict()
self.valid = False
class Trie:
def __init__(self):
self.root = trie_node()
def insert(self, word: str) -> None:
node = self.root
for l in word:
# print(l)
if l not in node.children1:
node.children1[l] = trie_node()
node = node.children1[l] # This is what I want to print out!! the value of children1.....
# print(node.children1[l])
node.valid = True
obj = Trie()
print(obj.insert('apple'))
如上,我想打印node的值,其实应该是一个一个字母的。。但是。。。
Traceback (most recent call last):
File "2.py", line 40, in <module>
print(obj.insert('apple'))
File "2.py", line 18, in insert
print(node.children1[l])
KeyError: 'a'
报错了。。不知道为什么,请教大佬指点!