gpt4 book ai didi

python - 从二叉树获取列表,TypeError

转载 作者:太空宇宙 更新时间:2023-11-03 17:48:17 25 4
gpt4 key购买 nike

因此,通过这个函数,我尝试遍历二叉树(按顺序)并将树中的值输出为列表。

使用此代码,我在第 14 行收到“TypeError: can only concatenate list (not “NoneType”) to list”(尽管问题源于第 6 行),我不确定这是为什么。

人们可以提供的任何帮助将不胜感激。

def getList(bST):
wholeList = []
if bST is None:
print []
if bST ['left']:
leftList = getList(bST['left'])
else:
leftList = []
rootList = [bST['data']]
if bST ['right']:
rightList = getList(bST['right'])
else:
rightList = []
wholeList = leftList + rootList + rightList
print wholeList

最佳答案

您忘记在最后返回wholeList。因此,getList() 返回 None。

关于python - 从二叉树获取列表,TypeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29451585/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com