children = {}
for obj in tree:
children.setdefault(obj.parent, []).append(obj)
def preorder(root, children):
yield root.value
for child in children.get(root, []):
for value in preorder(child, children):
yield value
for root in children[None]:
for value in preorder(root, children):
print value
我首先要创建一个更合适的数据结构,捕捉从父对象到子对象的链接:
您也可以在这里使用
collections.defaultdict
。在相关问题 更多 >
编程相关推荐