Problem Link:

http://oj.leetcode.com/problems/minimum-depth-of-binary-tree/

To find the minimum depth, we BFS from the root and record the depth. For each level we add 1 to the depth and return the depth value when we reach a leaf.

The python code is as follows.

# Definition for a  binary tree node
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution:
# @param root, a tree node
# @return an integer
def minDepth(self, root):
"""
BFS the tree and record the depth,
return this value for the first time that a leaf is reached.
"""
if not root:
return 0
depth = 1
q = [root]
while q:
new_q = []
for n in q:
if n.left == n.right == None:
return depth
if n.left:
new_q.append(n.left)
if n.right:
new_q.append(n.right)
q = new_q
depth += 1

【LeetCode OJ】Minimum Depth of Binary Tree的更多相关文章

  1. 【LeetCode练习题】Minimum Depth of Binary Tree

    Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...

  2. 【LeetCode OJ】Maximum Depth of Binary Tree

    Problem Link: https://oj.leetcode.com/problems/maximum-depth-of-binary-tree/ Simply BFS from root an ...

  3. 【LeetCode练习题】Maximum Depth of Binary Tree

    Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum depth is the n ...

  4. LeetCode OJ 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 OJ:Minimum Depth of Binary Tree(二叉树的最小深度)

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  6. 【leetcode❤python】 Maximum Depth of Binary Tree

    #-*- coding: UTF-8 -*- # Definition for a binary tree node.# class TreeNode(object):#     def __init ...

  7. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  8. LeetCode My Solution: Minimum Depth of Binary Tree

    Minimum Depth of Binary Tree Total Accepted: 24760 Total Submissions: 83665My Submissions Given a bi ...

  9. 【Leetcode】【Easy】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. GMF:如何在不打开Editor的情况下生成图片

    问题 GMF应用中,有时我们希望在不打开*DiagramEditor的情况下,从文件就能生成它的图片   解决方案 首先,从文件中构造DiagramImpl实例: TransactionalEditi ...

  2. Duilib实现类似电脑管家扫描目录效果

    实现原理: 1.后台开线程遍历目录,遍历出一个文件路径在界面上更新显示(通过发消息通知主界面) 2.需要扩展一下Duilib控件,在此我扩展了CLabelUI,重写了PaintText函数 扩展控件的 ...

  3. JavaScript的引擎机制

    var x=1; var y=0; var z=0; function add(n){n=n+1;return n;} y=add(x); function add(n){n=n+3;return n ...

  4. 初学画布canvas的chapter2

    文本 1.字体属性 context.font = [css font property] ——使用CSS规范,语法跟CSS字体速记符号一致 ——line-height无效,并永远忽略 Context. ...

  5. 《BI那点儿事》数据流转换——聚合

    聚合转换可以像T-SQL中的函数GROUP BY, Average, Minimum, Maximum, 和 Count一样对数据进行聚合运算.在图中可以看到数据以SampleID分组,对TotalS ...

  6. HDU-4534 郑厂长系列故事——新闻净化 AC自动机+DP

    题意:给定一些单词,这些单词必须要是一个目标串的子串,同时给定一些串,这些串不能够出现在目标串中,其余一些串只会带来不同附加值.现在问满足前两者的情况下,要求附加值最大.数据给定一个原始串,现在要求在 ...

  7. Oracle的表空间和数据文件

    一. 概念 表空间:是一个或多个数据文件的逻辑集合 表空间逻辑存储对象: 永久段-->如表与索引 临时段-->如临时表数据与排序段 回滚段-->用于事物回滚或闪回内存的撤销数据 表空 ...

  8. 基于注解的Spring AOP的配置和使用--转载

    AOP是OOP的延续,是Aspect Oriented Programming的缩写,意思是面向切面编程.可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种技术. ...

  9. Failed to execute goal org.codehaus.mojo:rpm-maven-plugin:2.1.1:rpm (default) on project **

    maven编译项目时报错:Failed to execute goal org.codehaus.mojo:rpm-maven-plugin:2.1.1:rpm (default) on projec ...

  10. MVC5 + EF6 入门完整教程二:从前端的UI开始

    从前端的UI开始 MVC分离的比较好,开发顺序没有特别要求,先开发哪一部分都可以,这次我们主要讲解前端UI的部分. ASP.NET MVC抛弃了WebForm的一些特有的习惯,例如服务器端控件,Vie ...