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. Android性能测试--内存

    前言: 近阶段都在探索android性能测试方面的东西,其中一个很重要的指标就是内存.对于内存,主要是一些gc是不是及时,或者说一些引用有没有及时释放,有没有导致oom或者内存持续增加导致卡顿,有没有 ...

  2. svn异常:subversion.javahl.ClientException

    使用svn时出现异常: INFO [org.netbeans.modules.subversion]: org.apache.subversion.javahl.ClientException: Pr ...

  3. shell之awk面试小案例

    [root@chavinking mnt]# cat textfile chavinking 1 2 3 4 5 6 nope 1 2 3 4 5 6 [root@chavinking mnt]# c ...

  4. SQL复制数据表 (select * into 与 insert into)

    select * into 目标表名 from 源表名 insert into 目标表名(fld1, fld2) select fld1, 5 from 源表名 以上两句都是将 源表 的数据插入到 目 ...

  5. JavaScript面向对象之创建类和方法

    一,js使用函数来定义类而不是像别的编程语言一样通过关键字class来定义,通过类本身(this)和原型(prototype)来完成面对对象编程! 示例1, //创建ElectronicSignatu ...

  6. JDBC---Mysql(2)

    SQL注入攻击: 用户可以提交一段数据库查询代码,根据程序返回的结果,获得某些他想知道的数据,这就是所谓的SQL注入攻击, 例如:判断username='a' or 'a'='a';  true从而为 ...

  7. MiniHook研究

    git hub 地址: https://github.com/RaMMicHaeL/minhook

  8. sql server创建临时表的两种写法和删除临时表

    --创建.删除临时表 --第一种方式 create table #tmp(name varchar(255),id int) --第二种方式 select count(id) as storyNum ...

  9. 自动化测试:java + testng + maven + reportng + jenkins + selenium (一)_基于win环境

    集成环境:jdk1.7 + tomcat1.7+ eclipse mars + maven + testng6.14.2 + selenium-java2.40.0 + reportng1.1.4 + ...

  10. mysql存储引擎的简介

    前言 数据库存储引擎是数据库底层软件组织,数据库管理系统(DBMS)使用数据引擎进行创建.查询.更新和删除数据.不同的存储引擎提供不同的存储机制.索引技巧.锁定水平等功能,使用不同的存储引擎,还可以 ...