111. Minimum Depth of Binary Tree 二叉树的最小深度
给定一个二叉树,找出其最小深度。
最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
说明: 叶子节点是指没有子节点的节点。
示例:
给定二叉树 [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(logN)O(\log N)O(logN)。
111. Minimum Depth of Binary Tree 二叉树的最小深度相关推荐
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 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 ...
- LeetCode: 111. Minimum Depth of Binary Tree
题目 Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bina ...
- 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 ...
- leetcode python3 简单题111. Minimum Depth of Binary Tree
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百一十一题 (1)题目 英文: Given a binary tree, fin ...
- 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 ...
- 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 ...
- 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 ...
- leetcode - Minimum Depth of Binary Tree
题目:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is th ...
最新文章
- CTF——angr使用学习记录
- 七、深入JavaScript函数,对象和作用域(三)
- 数据库SQL语言从入门到精通--Part 2--MySQL安装
- Util包中Arrays
- sql删除元组_Lecture #02: 中级SQL
- cad2010多个文件并排显示_便携式显示器清晰度参数,你知道吗?
- Android4.2 Input子系统
- static 结构体_C++基础-static
- 微软最强命令行工具发布,强势霸榜GitHub
- mysql切换alisql_安装AliSQL
- [渝粤教育] 西安交通大学 土力学 参考 资料
- SpringBoot非官方教程 | 第二十五篇:2小时学会springboot
- 笔记暂记15:陪集,商集
- 蓝蓝设计 扁平化界面风格的设计
- Win7安装.net 4.7.2
- 青出于蓝而胜于蓝,这是一款脱胎于Jupyter Notebook的新型编程环境
- 计算机语言中double是什么意思,C语言中double是什么意思?_后端开发
- JavaScript 基础(002_Event Bubbling)
- 文件查重FindDupFile
- 大数据-计算引擎:MapReduce、TEZ、Spark【Hive可选用的三大计算引擎】
热门文章
- Java 接口和抽象类的区别
- Python_面向对象_递归
- ArrayList刷题总结
- 一步步学习微软InfoPath2010和SP2010--第四章节--处理SP列表表单(6)--列表表单的局限...
- 【Vegas原创】终端服务器超出了最大允许连接数 解决方法
- 输入A、B,输出A+B
- bond的主备模式_linux-rhel7配置网卡bond双网卡主备模式
- 每日程序C语言47-找到年龄最大的人并输出
- Java黑皮书课后题第10章:*10.11(几何:Circle2D类)定义Circle2D类
- Java黑皮书课后题第5章:**5.7(金融应用:计算将来的学费)假设今年某大学的大学为10000美元,学费的年增长率为5%,一年后,学费将是10500美元。编写程序,计算10年后的学费,10~13费