【LeetCode OJ】Minimum Depth of Binary Tree
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的更多相关文章
- 【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 ...
- 【LeetCode OJ】Maximum Depth of Binary Tree
Problem Link: https://oj.leetcode.com/problems/maximum-depth-of-binary-tree/ Simply BFS from root an ...
- 【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 ...
- 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 ...
- 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 ...
- 【leetcode❤python】 Maximum Depth of Binary Tree
#-*- coding: UTF-8 -*- # Definition for a binary tree node.# class TreeNode(object):# def __init ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- LeetCode My Solution: Minimum Depth of Binary Tree
Minimum Depth of Binary Tree Total Accepted: 24760 Total Submissions: 83665My Submissions Given a bi ...
- 【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 ...
随机推荐
- Web网页的了解内容
一.因特网(互联网) 1.发展阶段: A 实验科研阶段 B社会化应用启动阶段 C社会化应用阶段 2.基本特征:资源共享 二.万维网 1.全称全球信息网(World Wide web),简称Web 2. ...
- javaScript实现修改输入框之后标红
<html> <title>实现标红</title> <script type="text/javascript"> functi ...
- 十六、Swing高级组件
1.利用JTable类直接创建表格 (1)创建表格 构造方法:JTable(Object rowData,Object[] columnNames) (2)定制表格 编辑:isCellEditable ...
- 使用c#访问脚本里变量的方法
首先,把要获取的变量权限定义为public类型变量. 方法一.public GameObject 另一个物体; //监视面板拖拽赋值 另一个物体.GetComponent<脚本>() ...
- 【Unity3D游戏开发】之利用语法糖添加自定义拓展方法(下) (十八)
首先需要声明的是“语法糖”这个词绝非贬义词,它可以给我带来方便,是一种便捷的写法,编译器会帮我们做转换:而且可以提高开发编码的效率,在性能上也不会带来损失.这让java开发人员羡慕不已,呵呵. 1. ...
- PHP 可变长度参数列表
In PHP 5.6 and later, argument lists may include the ... token to denote that the function accepts a ...
- FireFox中iframe的返回上页问题
-----已测试,可以使用----- 原文地址链接:http://fansofjava.iteye.com/blog/1019852 在写静态页面过程中遇到了FF的history.back()和his ...
- LTE Module User Documentation(翻译12)——X2切换(X2-based handover)
LTE用户文档 (如有不当的地方,欢迎指正!) 18 X2-based handover 正如 3GPP 定义的,切换是改变用户服务小区的连接方式的过程.这一过程中涉及的两个基站通常称为源基站和目 ...
- EasyUi 分页 和 表格数据加载
这里说明的是将说有数据先返回到前端再由前端去分页,性能可能没有先在后台分好页再返回给前端高 但如果操作不涉及大数据的话也没什么大问题,具体问题具体分析 要使用分页控件首先要声明初始化一下: //设置分 ...
- (九)errno和perror、标准IO
3.1.6.文件读写的一些细节3.1.6.1.errno和perror(1)errno就是error number,意思就是错误号码.linux系统中对各种常见错误做了个编号,当函数执行错误时,函数会 ...