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. day_5.10py 爬妹子图片 mm131

    #目前学的爬虫还有潭州教育的直播课,都是千篇一律的requests urllib 下面这个也是,还没有我后面的下载网易云歌单爽来都用到多线程了不过可以用协程,完全异步 1 #!/usr/bin/env ...

  2. I - 取石子游戏

    有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子:二是可以在两堆中同时取走相同数量的石子.最后把石子全部取完者为胜者. ...

  3. 【TOP100案例专访】当当网工程师林嘉琦谈双11大促经验及APM实践

    导读:第七届TOP100全球软件案例研究峰会将于11月30日-12月3日在北京国家会议中心举办,本届峰会以“释放AI生产力 让组织向智能化演进”为开幕式主题,旨在推动企业在趋势下拥抱AI.探索和思考A ...

  4. MySQL行(记录)的详细操作一 介绍 二 插入数据INSERT 三 更新数据UPDATE 四 删除数据DELETE 五 查询数据SELECT 六 权限管理

    MySQL行(记录)的详细操作 阅读目录 一 介绍 二 插入数据INSERT 三 更新数据UPDATE 四 删除数据DELETE 五 查询数据SELECT 六 权限管理 一 介绍 MySQL数据操作: ...

  5. tensorRT 使用tensorflow的pb问价构建推理

  6. 编译安装hadoop2.x

    1.Requirements: * Unix System * JDK 1.7+ * Maven 3.0 or later * Findbugs 1.3.9 (if running findbugs) ...

  7. iOS10原生的语音转文字功能

    #import <Foundation/Foundation.h> #import <Speech/Speech.h> @interface SpeechListener : ...

  8. MSSQL数据库优化经验

    数据库优化的目标无非是避免磁盘I/O瓶颈.减少CPU利用率和减少资源竞争.1. 在业务密集的SQL当中尽量不采用IN操作符2.  不使用not in 因为它不能应用表的索引.用not exists 或 ...

  9. iOS之分类(category)

    1.分类(category)的作用 1.1作用:可以在不修改原来类的基础上,为一个类扩展方法.1.2最主要的用法:给系统自带的类扩展方法. 2.分类中能写点啥? 2.1分类中只能添加“方法”,不能增加 ...

  10. php测试for/while/foreach循环速度对比

    对比代码先行贴上,有疑问或者有不同见解的希望可以提出,大家共同进步: //-------------------------------------$k=0;$checkTime = ['for'=& ...