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的更多相关文章

  1. [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 ...

  2. [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)

    [Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...

  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 ...

  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 111 minimum depth of binary tree

    problem description: Given a binary tree, find its minimum depth. The minimum depth is the number of ...

  6. 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 ...

  7. 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 ...

  8. (二叉树 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 ...

  9. 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 ...

随机推荐

  1. python数据类型之pandas—DataFrame

    DataFrame定义: DataFrame是pandas的两个主要数据结构之一,另一个是Series —一个表格型的数据结构 —含有一组有序的列 —大致可看成共享同一个index的Series集合 ...

  2. TOP100summit:【分享实录-Microsoft】基于Kafka与Spark的实时大数据质量监控平台

    本篇文章内容来自2016年TOP100summit Microsoft资深产品经理邢国冬的案例分享.编辑:Cynthia 邢国冬(Tony Xing):Microsoft资深产品经理.负责微软应用与服 ...

  3. HDU 1166 - 敌兵布阵 - [分块]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166 题解: 本题作为一个模板题,我用它来检验过总查询时间复杂度为 $O(q \log n)$ 的树状 ...

  4. [No0000CA]什么是“普瑞马法则”?以及 如何利用“普瑞马法则”,三步克服惰性

    一般在学习和生活中,我们都可能有这样的经验,就是当说想要作某件事情的时候,但过了好久发现还是没有做:或者觉得有力气使不出来:或者总觉得生活是灰色和抑郁的等等. 这类情况反映在生活中,就是生活好像总是被 ...

  5. shell脚本之通过发送带\n字符串或expect脚本实现交互输入自动化

    编写shell脚本难免遇到需要交互式输入指令的步骤: 方法一: # cat action.sh #!/bin/sh read -p "enter number:" no; read ...

  6. zookerper安装部署

    ********************单节点安装zk*************************上传zk安装包到服务器/mnt目录下: [root@chavin ~]$ ll /mnt/zoo ...

  7. git 错误 fatal: Not a valid object name: 'master'

    问题场景:新建git项目或刚为已存在项目创建了git仓库,想用git branch dev创建dev分支或用git checkout -b dev创建并切换到dev分支时报错. 原因:刚创建的git仓 ...

  8. MyCAT 在 Cobar 的基础上,完成了彻底的 NIO 通讯,并且合并了两个线程池

    研读: 1.http://www.mycat.io <Mycat权威指南> 第 2 章 Mycat 前世今生: 浏览: 深度认识 Sharding-JDBC:做最轻量级的数据库中间层 - ...

  9. Django 的操作

    安装: pip install Django 创建django工程 django-admin startproject  mysite python manage.py startapp blog / ...

  10. [development][dpdk][hugepage] 为不同的结点分配不同大小的大页内存

    这个事来自dpdk, 所以, 先参考. http://dpdk.org/doc/guides/linux_gsg/sys_reqs.html 当前, 假设你已经读过上边内容, 知道大页内存时候, dp ...