【发布时间】:2017-10-09 20:50:37
【问题描述】:
我正在遍历非二叉树,我有一个函数来计算节点的高度和子节点的数量。我想做的是首先按高度对节点的子节点进行排序,然后在每个高度组中按子节点数对其进行排序
例如:
a
/ \
b c
/|\ /
d e f g
/
h
所以当我遍历树时:
def orderTree(node):
if "children" in node:
if node['children']:
node['children'].sort(key=findHeight)
node['children'].sort(key=countChildren)
for child in node['children']:
print(child['name'])
orderTree(child)
我使用此代码 = > a,c,g,h,b,d,e,f 但我需要的是 = > a,b,d,e,f,c,g,h
知道如何对 python 列表中的已排序项目组进行排序吗?
【问题讨论】:
标签: python algorithm sorting data-structures tree