2024-10-02 18:15:56 发布
网友
这是我解决这个问题的代码
def height(t): """Return the depth of the deepest node in the tree.""" if isinstance(t, list): t = t[1:] if t != empty: return 1 + max([height(x) for x in t]) return 0
但是它没有像我期望的那样返回值。有人能弄明白吗? 谢谢。 我的树法是:
height函数的参数t应该是一个树,但是您期望的是一个列表if isinstance(t, list),显然,它不是list,所以只返回0。在
height
if isinstance(t, list)
因为你的height函数是错误的。height函数甚至没有调用entry或{}函数。在
entry
应获取树的子树,如果为空,则返回1。如果不为空,则返回t的子树高度的1+max
我不粘贴代码,请您自己检查。在
height
函数的参数t应该是一个树,但是您期望的是一个列表if isinstance(t, list)
,显然,它不是list,所以只返回0。在因为你的}函数。在
height
函数是错误的。height
函数甚至没有调用entry
或{应获取树的子树,如果为空,则返回1。如果不为空,则返回t的子树高度的1+max
我不粘贴代码,请您自己检查。在
相关问题 更多 >
编程相关推荐