作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


[LeetCode]

题目地址:https://leetcode.com/problems/minimum-depth-of-binary-tree/

Total Accepted: 70767 Total Submissions: 243842 Difficulty: Easy

题目描述

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.

题目大意

求根节点到最近的叶子节点的高度。

解题方法

DFS

运用递归,递归当前和 左子树和右子树的深度,某节点的左右子树都是空的时候,说明是叶子。计算根节点到此叶子的深度。

注意:如果是叶子,那么此叶子的深度是1.

同时注意:如果有一方的某一子树为空,那么它的深度为0,但不应该进入树的深度的计算当中去。

Better solution:用HashMap存储已经遍历过的树,减少空间复杂度。实现效率的提高。

  1. 当root为空的时候直接返回0,因为MIN赋值很大,所以如果不单独预判的话会返回MIN
  2. 判断树的深度应该到叶子节点,也就是左右子结点都为空的那个结点
  3. 树的深度的根节点深度为1

递归解法如下:

# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
def minDepth(self, root):
"""
:type root: TreeNode
:rtype: int
"""
if not root: return 0
que = collections.deque()
que.append(root)
depth = 1
while que:
size = len(que)
for _ in range(size):
node = que.popleft()
if not node: continue
if not node.left and not node.right:
return depth
que.append(node.left)
que.append(node.right)
depth += 1
return depth

BFS

其实BFS更简单,因为发现这层有个叶子的话,就直接返回就行了。

# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
def minDepth(self, root):
"""
:type root: TreeNode
:rtype: int
"""
if not root: return 0
left = self.minDepth(root.left)
right = self.minDepth(root.right)
if not left:
return right + 1
if not right:
return left + 1
return 1 + min(left, right)

日期

2015/9/17 10:49:04
2018 年 11 月 24 日 —— 周六快乐

【LeetCode】111. Minimum Depth of Binary Tree 解题报告(Python)的更多相关文章

  1. [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)

    [Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...

  2. [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 ...

  3. 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 ...

  4. Leetcode 111 Minimum Depth of Binary Tree 二叉树

    找出最短的从叶子到根的路径长 可以回忆Maximum Depth of Binary Tree的写法,只不过在!root,我把它改成了10000000,还有max函数改成了min函数,最后的值如果是1 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. leetcode 111 minimum depth of binary tree

    problem description: Given a binary tree, find its minimum depth. The minimum depth is the number of ...

  9. 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 ...

随机推荐

  1. 【百奥云GS专栏】1-全基因组选择介绍

    目录 什么是基因组选择? 基因组选择技术的发展 基因组选择的原理和流程 基因组选择的模型 基因组选择的展望 参考资料 什么是基因组选择? 基因组选择(Genomic Selection,简称GS)这一 ...

  2. Golang gRPC调试工具

    目录 Golang gRPC调试工具 1. 命令行工具 grpcurl 1.1 安装 1.2 验证 1.3 注册反射 1.4 使用示例 2. web调试工具grpcui 2.1 安装 2.2 验证 2 ...

  3. mongodb存储的基本使用

    Python连接mongodb一般使用pymongo模块 1. pymongo模块的简单使用 ### MongoDB存储 ## 连接MongoDB import pymongo # 建立连接对象,2种 ...

  4. 学习java的第十一天

    一.今日收获 1.学习java完全学习手册2.9.3循环结构的内容并验证例题 2.观看哔哩哔哩上的教学视频 二.今日问题 1.基本没有 三.明日目标 1.继续完成2.9.3循环结构的例题 2.哔哩哔哩 ...

  5. mysql数据操作语言DML

    插入insert 插入方式1 语法: insert into 表名(列名,....) values(值1,....) 说明: 1.插入的值的类型要与列的类型一致或兼容 2.可以为null的值:①列写了 ...

  6. Hbase与Phoenix整合

    目录 一.简介 二.安装 三.Phoenix Shell操作 SCHEMA操作 1.创建schema 2.使用schema 3.删除schema 表操作 1.显示所有表 2.创建表 3.表数据的增删改 ...

  7. Android Handler 消息机制原理解析

    前言 做过 Android 开发的童鞋都知道,不能在非主线程修改 UI 控件,因为 Android 规定只能在主线程中访问 UI ,如果在子线程中访问 UI ,那么程序就会抛出异常 android.v ...

  8. 【Linux】【Services】【SaaS】Spinnaker

    1. 简介 1.1. 说明: Spinnaker 是 Netflix 的开源项目,是一个持续交付平台,它定位于将产品快速且持续的部署到多种云平台上.Spinnaker 通过将发布和各个云平台解耦,来将 ...

  9. Undefined symbols for architecture arm64:问题

    Undefined symbols for architecture arm64: "_sqlite3_prepare_v2", referenced from: +[HMJSch ...

  10. Xcode中匹配的配置包的存放目录

    /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport