0
这里t
表示一棵树。树可以有一个孩子的列表。 act
是一种行为,如print
。我的代码显示根目录t
以下的所有级别的正确顺序。我如何修改代码以包含t
本身?如何编写表示二叉树级别顺序的代码?
def levelorder_visit(t, act):
"""
Visit every node in Tree t in level order and act on the node
as you visit it.
@param Tree t: tree to visit in level order
@param (Tree)->Any act: function to execute during visit
>>> t = descendants_from_list(Tree(0), [1, 2, 3, 4, 5, 6, 7], 3)
>>> def act(node): print(node.value)
>>> levelorder_visit(t, act)
0
1
2
3
4
5
6
7
"""
if t:
for x in t.children:
act(x)
for i in t.children:
levelorder_visit(i,act)
###prints out "1,2,3,4,5,6,7" for the above docstring example
对不起,我认为你的代码是预先订购的。 – 1412
对不起,没有想到这一点。查看更正的代码。 – Muctadir