【发布时间】:2021-07-30 16:13:16
【问题描述】:
谁能帮我弄清楚我的代码有什么问题?
这个测试用例不起作用 - [2,null,3,null,4,null,5,null,6] - 我的代码返回 0 而不是 5。不知道为什么。
class Solution:
def minDepth(self, root: TreeNode) -> int:
if root.left is None and root.right is None:
return 0
elif root.left is None and root.right is not None:
return self.minDepth(root.right)
elif root.right is None and root.left is not None:
return self.minDepth(root.left)
else:
return 1 + min(self.minDepth(root.left), self.minDepth(root.right))
谢谢!
【问题讨论】:
-
为什么在所有情况下都不给递归调用加 1?
-
如果您只是调试并单步执行代码,并查看变量具有函数调用返回的值,这将很容易解决。一旦你这样做了,你就会发现没有理由发布这个问题。
标签: python recursion binary-tree