给定一个二叉树,找出其最小深度。

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

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

示例:

给定二叉树 [3,9,20,null,null,15,7],

    3/ \9  20/  \15   7

返回它的最小深度  2.

DFS

首先可以想到使用深度优先搜索的方法,遍历整棵树,记录最小深度。

对于每一个非叶子节点,我们只需要分别计算其左右子树的最小叶子节点深度。这样就将一个大问题转化为了小问题,可以递归地解决该问题。

Code

    def minDepth(self, root: TreeNode) -> int:if not root:return 0if not root.left and not root.right:return 1minDepth = 10 ** 9if root.left:minDepth = min(self.minDepth(root.left), minDepth)if root.right:minDepth = min(self.minDepth(root.right), minDepth)return minDepth + 1

复杂度分析

  • 时间复杂度:O(N)O(N)O(N),其中 NNN 是树的节点数。对每个节点访问一次。

  • 空间复杂度:O(H)O(H)O(H),其中 HHH 是树的高度。空间复杂度主要取决于递归时栈空间的开销,最坏情况下,树呈现链状,空间复杂度为 O(N)O(N)O(N)。平均情况下树的高度与节点数的对数正相关,空间复杂度为 O(log⁡N)O(\log N)O(logN)。

111. Minimum Depth of Binary Tree 二叉树的最小深度相关推荐

  1. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  2. 111. Minimum Depth of Binary Tree

    1.问题描述 Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along t ...

  3. LeetCode: 111. Minimum Depth of Binary Tree

    题目 Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bina ...

  4. LeetCode 111. Minimum Depth of Binary Tree (二叉树最小的深度)

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  5. leetcode python3 简单题111. Minimum Depth of Binary Tree

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百一十一题 (1)题目 英文: Given a binary tree, fin ...

  6. LeetCode 111. Minimum Depth of Binary Tree

    原题 Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the s ...

  7. LeetCode 111. Minimum Depth of Binary Tree--Java, Python解法--二叉树最小高度--迭代,递归

    题目地址:Minimum Depth of Binary Tree - LeetCode Given a binary tree, find its minimum depth. The minimu ...

  8. LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree

    LeetCode:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth ...

  9. leetcode - Minimum Depth of Binary Tree

    题目:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is th ...

最新文章

  1. CTF——angr使用学习记录
  2. 七、深入JavaScript函数,对象和作用域(三)
  3. 数据库SQL语言从入门到精通--Part 2--MySQL安装
  4. Util包中Arrays
  5. sql删除元组_Lecture #02: 中级SQL
  6. cad2010多个文件并排显示_便携式显示器清晰度参数,你知道吗?
  7. Android4.2 Input子系统
  8. static 结构体_C++基础-static
  9. 微软最强命令行工具发布,强势霸榜GitHub
  10. mysql切换alisql_安装AliSQL
  11. [渝粤教育] 西安交通大学 土力学 参考 资料
  12. SpringBoot非官方教程 | 第二十五篇:2小时学会springboot
  13. 笔记暂记15:陪集,商集
  14. 蓝蓝设计 扁平化界面风格的设计
  15. Win7安装.net 4.7.2
  16. 青出于蓝而胜于蓝,这是一款脱胎于Jupyter Notebook的新型编程环境
  17. 计算机语言中double是什么意思,C语言中double是什么意思?_后端开发
  18. JavaScript 基础(002_Event Bubbling)
  19. 文件查重FindDupFile
  20. 大数据-计算引擎:MapReduce、TEZ、Spark【Hive可选用的三大计算引擎】

热门文章

  1. Java 接口和抽象类的区别
  2. Python_面向对象_递归
  3. ArrayList刷题总结
  4. 一步步学习微软InfoPath2010和SP2010--第四章节--处理SP列表表单(6)--列表表单的局限...
  5. 【Vegas原创】终端服务器超出了最大允许连接数 解决方法
  6. 输入A、B,输出A+B
  7. bond的主备模式_linux-rhel7配置网卡bond双网卡主备模式
  8. 每日程序C语言47-找到年龄最大的人并输出
  9. Java黑皮书课后题第10章:*10.11(几何:Circle2D类)定义Circle2D类
  10. Java黑皮书课后题第5章:**5.7(金融应用:计算将来的学费)假设今年某大学的大学为10000美元,学费的年增长率为5%,一年后,学费将是10500美元。编写程序,计算10年后的学费,10~13费