二元树的最小深度

发布于 2025-01-28 15:27:43 字数 995 浏览 1 评论 0 原文

我正在尝试编写代码以找到二进制树的最小深度。

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution(object):
        
    def minDepth(self, node):
        if node is None:
            return 0
    
        else :
            # Compute the depth of each subtree
            lDepth = self.minDepth(node.left)
            rDepth = self.minDepth(node.right)
            return min(lDepth, rDepth) + 1

但是,此解决方案在某些测试用例上不起作用,例如高度不平衡的二进制树,它将其转变为链接列表(ex [2,none,3,3,none,4,4,无,5,无,6]

不计数

     2
    / \
       3
      / \
         4
        / \
           5
          / \
             6

最小深度为5(因为没有孩子 'M亏本,为什么该解决方案不能充分解决此用例?

I'm trying to write code to find the minimum depth of a binary tree.
https://leetcode.com/problems/minimum-depth-of-binary-tree/

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution(object):
        
    def minDepth(self, node):
        if node is None:
            return 0
    
        else :
            # Compute the depth of each subtree
            lDepth = self.minDepth(node.left)
            rDepth = self.minDepth(node.right)
            return min(lDepth, rDepth) + 1

However, this solution does not work on some test cases, such as a highly unbalanced binary tree, which devolves to a linked list (ex [2, None, 3, None, 4, None, 5, None, 6]

The minimum depth is 5 (as None children do not count.) However, my solution returns 1, so it must be treating the left child of 2 as the minimum depth leaf node.

     2
    / \
       3
      / \
         4
        / \
           5
          / \
             6

I'm at a loss, why does this solution not adequately address this use case?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萌辣 2025-02-04 15:27:43

我认为这是因为 min 函数。从2开始,您的代码检查左侧为0,然后检查右检查并递归检查并返回4。代码> 1 。

I think its because of the min function. Starting from 2, your code checks the left which is 0, then checks right which is recursively checked and returns 4. However, you call min(0, 4)+1 which gives you an output of 1.

旧情勿念 2025-02-04 15:27:43

您的代码在 none 上停止递归,这不是 treenode ,显然,对于此类对象而言,术语“深度”是未定义的。尝试在所谓的“叶子”上停止递归:没有孩子的节点。查看我的解决方案有关此问题:

def is_leaf(node: TreeNode):
    return node.left is None and node.right is None

def min_depth(root: TreeNode):
    if is_leaf(root):
        return 1
    not_none_children = (
        child if child is not None
        for child in (root.left, root.right)]
    )
    return min(min_depth(child) for child in not_none_children) + 1

Your code stops recursion on None which is not a TreeNode, obviously, the term "depth" is undefined for such objects. Try to stop your recursion on so-called "leafs": nodes without children. Check out my solution for this problem:

def is_leaf(node: TreeNode):
    return node.left is None and node.right is None

def min_depth(root: TreeNode):
    if is_leaf(root):
        return 1
    not_none_children = (
        child if child is not None
        for child in (root.left, root.right)]
    )
    return min(min_depth(child) for child in not_none_children) + 1
狼亦尘 2025-02-04 15:27:43

@Avinash,感谢您的边缘案例。
问题@kellybundy

class Solution(object):
    
    def minDepth(self, node):
        # no node (dead branch)
        if node is None:
            return 0
        
        lDepth = self.minDepth(node.left)
        rDepth = self.minDepth(node.right)        
        
        if node.left and node.right: 
            return min(lDepth, rDepth) + 1
        
        else: #prevents counting first dead branch as minDepth
            return max(lDepth, rDepth) + 1 

@Avinash, thanks for ID'ing the edge case.
And the problem @kellyBundy https://leetcode.com/problems/minimum-depth-of-binary-tree/submissions/

class Solution(object):
    
    def minDepth(self, node):
        # no node (dead branch)
        if node is None:
            return 0
        
        lDepth = self.minDepth(node.left)
        rDepth = self.minDepth(node.right)        
        
        if node.left and node.right: 
            return min(lDepth, rDepth) + 1
        
        else: #prevents counting first dead branch as minDepth
            return max(lDepth, rDepth) + 1 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文