作者: 负雪明烛
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. 【R】clusterProfiler的GO/KEGG富集分析用法小结

    前言 关于clusterProfiler这个R包就不介绍了,网红教授宣传得很成功,功能也比较强大,主要是做GO和KEGG的功能富集及其可视化.简单总结下用法,以后用时可直接找来用. 首先考虑一个问题: ...

  2. EXCEL-批量修改列宽

    WPS:先用鼠标选中一列,然后,长按ctrl键并且用鼠标选中剩余想要统一列宽的列,松开ctrl键,鼠标落在刚选中的任意一列的抬头上,鼠标右键,选择列宽,设置统一列宽即可.

  3. liveBOS环境搭建

    环境搭建:1.准备jdk1.6及以上版本oracle11gplsqlsql脚本(oracle_init.sql,oracle_insert.sql)livebos_tomcatlivebos的授权文件 ...

  4. 🏆【Alibaba中间件技术系列】「Sentinel技术专题」分布式系统的流量防卫兵的基本介绍(入门源码介绍)

    推荐资料 官方文档 官方demo Sentinel 是什么? 随着微服务的流行,服务和服务之间的稳定性变得越来越重要.Sentinel 以流量为切入点,从流量控制.熔断降级.系统负载保护等多个维度保护 ...

  5. A Child's History of England.30

    CHAPTER 10 ENGLAND UNDER HENRY THE FIRST, CALLED FINE-SCHOLAR Fine-scholar, on hearing of the Red Ki ...

  6. day22面向对象编程思想

    day22面向对象编程思想 1.面向过程 面向过程: 核心是"过程"二字 过程的终极奥义就是将程序流程化 过程是"流水线",用来分步骤解决问题的 面向对象: 核 ...

  7. android 跳到应用市场给软件评分

    1 String packetName = this.getPackageName(); 2 Uri uri = Uri.parse("market://details?id=" ...

  8. d3 CSS

    CSS的inline.block与inline-block 块级元素(block):独占一行,对宽高的属性值生效:如果不给宽度,块级元素就默认为浏览器的宽度,即就是100%宽. 行内元素(inline ...

  9. Playing with Destructors in C++

    Predict the output of the below code snippet. 1 #include <iostream> 2 using namespace std; 3 4 ...

  10. Function overloading and const keyword

    Predict the output of following C++ program. 1 #include<iostream> 2 using namespace std; 3 4 c ...