1class TrieNode:
2 def __init__(self):
3 self.children = {}
4 self.is_end = False
5
6def delete(root, word, depth=0):
7 if depth == len(word):
8 root.is_end = False
9 return len(root.children) == 0
10
11 char = word[depth]
12 if char not in root.children:
13 return False
14
15 should_delete = delete(root.children[char], word, depth + 1)
16 if should_delete:
17 del root.children[char]
18 return len(root.children) == 0 and not root.is_end
19 return False
no lines flagged
#033PracticeHard35 min · 200 XP
Trie Delete Removes Shared Prefix
Deleting one word from a trie removes other words that share a prefix with it.
Flagged linesNo lines flagged yet
What's wrong?
Flag a line or write a note to submit.