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. mysql之常用操作

    1. 数据库介绍 什么是数据库? 数据库(Database)是按照数据结构来组织.存储和管理数据的仓库,每个数据库都有一个或多个不同的API用于创建,访问,管理,搜索和复制所保存的数据.我们也可以将数 ...

  2. python 学习笔记十三 JQuery(进阶篇)

    jQuery 是一个 JavaScript 库. jQuery 极大地简化了 JavaScript 编程. 安装jQuery 有两个版本的 jQuery 可供下载: Production versio ...

  3. 【noip新手入门向】OpenJudge1.3-14大象喝水

    一.写在前面 我也不知道我为什么要写这个鬼畜的东西←_←才不是为了水blog量什么的(划掉),其实是为了明天给学弟学妹们传教准备. 这道题对完全对c语言没有概念的小萌新们极度友好,可以锻炼小萌新们的代 ...

  4. 十六、Swing高级组件

    1.利用JTable类直接创建表格 (1)创建表格 构造方法:JTable(Object rowData,Object[] columnNames) (2)定制表格 编辑:isCellEditable ...

  5. Adobe Edge Animate CC 不再开发更新!

    Adobe Edge Animate CC停止开发更新! http://blogs.adobe.com/edge/2015/11/30/update-about-edge-tools-services ...

  6. javascript 分页组件

    自己写的一个简单的分页组件,主要功能还有实现都在JS中,html页面中只用增加一个放置生成分页的DIV,并给定容器的id. html结构如下: <ul class="paginatio ...

  7. [bzoj1452][JSOI2009]Count(树状数组)

    1452: [JSOI2009]Count Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 2057  Solved: 1216[Submit][Stat ...

  8. treeview递归加载

    实体类: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ...

  9. MultiDex到底有多坑

    google为什么要引入MultiDex? dex指令是用16位寄存器来保存dex中的方法数,所以限制了在apk 中最大的方法数为65535,当超过这个最大值在编译的时候会报 方法数超标的错误. 如何 ...

  10. mysql跟踪和日志

    mysql 打开 general log 后,所有的查询语句都会记录在 general log 文件,文件为只读方式,但这样general log文件会非常大,所以默认是关闭的. 但有时需要查错等原因 ...