在树形结构中,我试图找到一个分支的所有叶子。这是我写的:
def leafs_of_branch(node,heads=[]):
if len(node.children()) == 0:
heads.append(str(node))
else:
for des in node.children():
leafs_of_branch(des)
return heads
leafs_of_branch(node)
我不知道为什么,但对我来说感觉不对。它有效,但我想知道是否有更好的方法来使用递归而不创建heads
参数。