返回中最深节点的深度

2024-10-02 18:15:56 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我解决这个问题的代码

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

但是它没有像我期望的那样返回值。有人能弄明白吗? 谢谢。 我的树法是:

^{pr2}$

Tags: ofthe代码innodetreereturnif
1条回答
网友
1楼 · 发布于 2024-10-02 18:15:56

height函数的参数t应该是一个树,但是您期望的是一个列表if isinstance(t, list),显然,它不是list,所以只返回0。在

因为你的height函数是错误的。height函数甚至没有调用entry或{}函数。在

应获取树的子树,如果为空,则返回1。如果不为空,则返回t的子树高度的1+max

我不粘贴代码,请您自己检查。在

相关问题 更多 >