我只是好奇是否有办法比较两个尝试数据结构的相似性?
trie1 trie2
root root
/ | / |
m b m b
| | | |
a o a o
| \ | | |
t x b x b
def compare_trie(trie1, trie2):
pass
Output["max","bob"]
Run Code Online (Sandbox Code Playgroud)
编辑:到目前为止,我尝试实现 dfs 算法,但对如何管理不同尝试的两个堆栈感到震惊
我尝试过的代码仍然通过管理两个堆栈进行两次不同的尝试而感到震惊:
def compareTrie(trie1, trie2):
dfsStack = []
result = []
stack1 = [x for x in trie1.keys()]
stack2 = [y for y in trie2.keys()]
similar = list(set(stack1) & set(stack2))
dfsStack.append((similar, result))
while (dfsStack):
current, result = dfsStack.pop()
print(current, result)
result.append(current)
for …Run Code Online (Sandbox Code Playgroud)