[LeetCode] 111. Minimum Depth of Binary Tree_Easy tag:DFS
Given a binary tree, find its minimum depth.
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
Note: A leaf is a node with no children.
Example:
Given binary tree [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
return its minimum depth = 2.
2018/0813 update: 不能直接用l or r, 因为我们要先选小的, 如果小的是空, 然后才需要去判断那个大的. 所以需要min 及max, 而且min应该在前面.
思路为DFS recursive, 但是需要注意的是, 比如[1,2] 应该return 2, 但是上面知会return 1. 所以需要return min(l,r) or max(l, r), 因为有一边有, 另一边没有的时候要返回有的.
2020/0509 update: 直接用BFS,碰到第一个leaf,return当时的height即可。
1. Constraints
1) can be none => 0
2. Ideas
BFS T: O(n) S: O(n)
DFS recursive T: O(n) S: O(n)
3, Code
class Solution:
def minDepth(self, root):
if not root: return 0
l, r = self.minDepth(root.left), self.minDepth(root.right)
return 1+ (min(l, r) or max(l, r))
=> more elegant
class Solution:
def minDepth(self, root):
if not root: return 0
d = map(self.minDepth, (root.left, root.right))
return 1 + (min(d) or max(d))
利用BFS
class Solution:
def miniDepth(self, root):
if not root: return 0
queue = collections.deque([(root, 1)])
while queue:
node, height = queue.popleft()
if not node.left and not node.right:
return height
if node.left:
queue.append((node.left, height + 1))
if node.right:
queue.append((node.right, height + 1))
4. Test cases
1) [1,2]
2) [3,9,20,null,null,15,7]
[LeetCode] 111. Minimum Depth of Binary Tree_Easy tag:DFS的更多相关文章
- [LeetCode] 104. Maximum Depth of Binary Tree_Easy tag: DFS
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)
[Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...
- [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 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 111 minimum depth of binary tree
problem description: Given a binary tree, find its minimum depth. The minimum depth is the number of ...
- leetcode 111 Minimum Depth of Binary Tree ----- java
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- Java [Leetcode 111]Minimum Depth of Binary Tree
题目描述: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along th ...
- (二叉树 BFS DFS) 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 ...
- Java for 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 ...
随机推荐
- Nodejs----学习路线
一:javascript基础 1.语法 2.数据类型 3.操作符 4.语句 5.函数 6.变量 7.Object 类型 8.基本包装类型 9.Global 对象 10.Math 对象 11.初始化和检 ...
- webpack.DefinePlugin
通过配置了DefinePlugin,那么这里面的标识就相当于全局变量,你的业务代码可以直接使用配置的标识.比如,你通过下面的设置: // webpack.config.js new webpack.D ...
- 使用graalvm.js调用promise
前提 1.JDK1.8 2.引入jar包 <!--graalvm.js --> <dependency> <groupId>org.graalvm.js</g ...
- vsftpd服务安装配置
服务器:centos6.5 32位 192.168.1.114 1.安装 yum -y install vsftpd 2.启动 /etc/init.d/vsftpd start 3.配置 配置文件 ...
- 数学用语中的 透明 transitive property
1.透明 https://en.wikipedia.org/wiki/Equivalence_relation In mathematics, an equivalence relation is a ...
- 2015年蓝桥杯省赛A组c++第8题(迭代法)
/* 乐羊羊饮料厂正在举办一次促销优惠活动.乐羊羊C型饮料,凭3个瓶盖可以再换一瓶C型饮料, 并且可以一直循环下去(但不允许暂借或赊账). 请你计算一下,如果小明不浪费瓶盖,尽量地参加活动,那么,对于 ...
- Xcode 编辑器之Workspace,Project,Scheme,Target
一,前言 最近老是突然对Workspace,Project,Scheme,Target四者的关系有些疑惑,所以查阅资料总结一下. 二,Workspace,Project,Scheme,Target四者 ...
- 转:Java 集合详解
原文地址:https://www.cnblogs.com/ysocean/p/6555373.html 一.集合的由来 通常,我们的程序需要根据程序运行时才知道创建多少个对象.但若非程序运行,程序开发 ...
- Java如何写Common直接调用
一:新建Class类,命名为:Common 1. 写public static 公共的静态方法: 2. 直接用 Common.方法名 就可以直接调用. 例如:写一个获取当前星期的方法. /** ...
- RequireJs的理解
什么是RequireJs RequireJS 是一个JavaScript模块加载器. 在ES6出现之前,JS不像其他语言同样拥有“模块”这一概念,于是为了支持JS模块化,出现了各种各样的语言工具,如w ...