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来完成,万能的递归啊。首先判空,若当前结点不存在,直接返回0。然后看若左子结点不存在,那么对右子结点调用递归函数,并加1返回。反之,若右子结点不存在,那么对左子结点调用递归函数,并加1返回。若左右子结点都存在,则分别对左右子结点调用递归函数,将二者中的较小值加1返回即可,参见代码如下:

解法一:

class Solution {
public:
int minDepth(TreeNode* root) {
if (!root) return ;
if (!root->left) return + minDepth(root->right);
if (!root->right) return + minDepth(root->left);
return + min(minDepth(root->left), minDepth(root->right));
}
};

我们也可以是迭代来做,层序遍历,记录遍历的层数,一旦我们遍历到第一个叶结点,就将当前层数返回,即为二叉树的最小深度,参见代码如下:

解法二:

class Solution {
public:
int minDepth(TreeNode* root) {
if (!root) return ;
int res = ;
queue<TreeNode*> q{{root}};
while (!q.empty()) {
++res;
for (int i = q.size(); i > ; --i) {
auto t = q.front(); q.pop();
if (!t->left && !t->right) return res;
if (t->left) q.push(t->left);
if (t->right) q.push(t->right);
}
}
return -;
}
};

类似题目:

Binary Tree Level Order Traversal

Maximum Depth of Binary Tree

参考资料:

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

https://leetcode.com/problems/minimum-depth-of-binary-tree/discuss/36153/My-concise-c%2B%2B-solution

https://leetcode.com/problems/minimum-depth-of-binary-tree/discuss/36071/BFS-C%2B%2B-8ms-Beats-99.94-submissions

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Minimum Depth of Binary Tree 二叉树的最小深度的更多相关文章

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

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

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

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

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

  4. [Leetcode] The 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. 111 Minimum Depth of Binary Tree 二叉树的最小深度

    给定一个二叉树,找出其最小深度.最小深度是从根节点到最近叶节点的最短路径的节点数量.详见:https://leetcode.com/problems/minimum-depth-of-binary-t ...

  6. [LeetCode] 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. LeetCode:111_Minimum Depth of Binary Tree | 二叉树的最小深度 | Easy

    要求:此题正好和Maximum Depth of Binary Tree一题是相反的,即寻找二叉树的最小的深度值:从根节点到最近的叶子节点的距离. 结题思路:和找最大距离不同之处在于:找最小距离要注意 ...

  8. LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree

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

  9. [LeetCode] Maximum Depth of Binary Tree 二叉树的最大深度

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

随机推荐

  1. centos下MYSQL 没有ROOT用户的解决方法。

    SbTest for using sysbench creating scritps: sysbench --test=oltp --oltp-table-size=100000 --mysql-db ...

  2. 简述9种社交概念 SNS究竟用来干嘛?

    1.QQ 必备型交流工具基本上每一个网民最少有一个QQ,QQ已经成为网民的标配,网络生活中已经离不开QQ了.虽然大家嘴上一直在骂 QQ这个不好,那个不对,但是很少有人能彻底离开QQ.QQ属于IM软件, ...

  3. 【转】MVC、MVP与MVT

    MVC是Model-View-Control的缩写,Model指的是数据层,View指的是UI层,Control指的是控制层,这三层之间彼此联系.View层的用户行为,触发Control层,Contr ...

  4. 微信小程序demo理解

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Verdana } p.p2 { margin: 0.0px 0.0px 0.0px 0.0p ...

  5. JDK1.7.0_45源码阅读<java.lang.Boolean>

    本文适合的人群 其实感觉写这个标题的内容没有必要,只要你觉得对你有帮助那么就适合你,对你没帮助那么就不适合你.毕竟我不是专业作者,但咱会尽力的.其实最重要的一点是我不希望浪费您宝贵时间. 简要把内容在 ...

  6. ArrayList LinkedList源码解析

    在java中,集合这一数据结构应用广泛,应用最多的莫过于List接口下面的ArrayList和LinkedList; 我们先说List, public interface List<E> ...

  7. 关于mysql 和Oracle的一大堆麻烦问题的解决方案

    [INS-20802] Oracle Net Configuration Assistant 失败 在百度上找了半天并没有找到可靠的解决方案,最后是可以安装完成的,之后我 通过SQL Plus连接就报 ...

  8. JavaScript 数据属性和访问器属性

    在JavaScript中对象被定义为"无序属性的集合,其属性可以包含基本值.对象或函数."通俗点讲,我们可以把对象理解为一组一组的名值对,其中值可以是数据或函数. 创建自定义对象通 ...

  9. Linux常用命令大全

    系统信息 arch 显示机器的处理器架构(1)  uname -m 显示机器的处理器架构(2)  uname -r 显示正在使用的内核版本 dmidecode -q 显示硬件系统部件 - (SMBIO ...

  10. android okvolley框架搭建

    最近新出了很多好东西都没时间去好好看看,现在得好好复习下了,记下笔记 记得以前用的框架是android-async-http,volley啊,或者其它的,然后后面接着又出了okhttp,retrofi ...