首页 > 其他 > 详细

leetcode_111. 二叉树的最小深度

时间:2020-11-29 10:29:42      阅读:24      评论:0      收藏:0      [点我收藏+]
给定一个二叉树,找出其最小深度。

最小深度是从根节点到最近叶子节点的最短路径上的节点数量。

说明:叶子节点是指没有子节点的节点。

 

示例 1:


输入:root = [3,9,20,null,null,15,7]
输出:2
示例 2:

输入:root = [2,null,3,null,4,null,5,null,6]
输出:5
 

提示:

树中节点数的范围在 [0, 105] 内
-1000 <= Node.val <= 1000

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/minimum-depth-of-binary-tree
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def minDepth(self, root: TreeNode) -> int:
        if not root :
            return 0
        if not root.left and not root.right:
            return 1
        min_height=10**8
        if root.left:
            min_height=min(self.minDepth(root.left),min_height)
        if root.right:
            min_height=min(self.minDepth(root.right),min_height)
        return 1+min_height

leetcode_111. 二叉树的最小深度

原文:https://www.cnblogs.com/hqzxwm/p/14055485.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!