(二叉树 BFS DFS) leetcode 111. Minimum Depth of Binary Tree

时间:2022-08-27 16:34:00

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,也可以用BFS,我这个题先用BFS写,然后用DFS。

要注意,注意,这个最小深度指的是,从根节点到最近的叶子节点的距离,比如[1,1]这个最小深度就是2!!!,同样,最大深度也是2。

[1,1]图:

(二叉树 BFS DFS) leetcode 111. Minimum Depth of Binary Tree

所以在用BFS时,只有这个结点的左子节点和右子节点同时为NULL时,才能确定为叶子节点,才能返回sum(表示最小深度)。

同理,在用DFS时,当左子节点为NULL,而右子节点不为空,那就不是叶子节点,还得计算。同理,如果右子节点为NULL,而左子节点也不是,就得继续递归下去。

C++代码:

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int minDepth(TreeNode* root) {
queue<TreeNode*> q;
if(!root)
return ;
q.push(root);
int sum = ;
while(!q.empty()){
sum++;
for(int i = q.size(); i > ; i--){ //必须写循环,如果不写,对于[1,2,3,4,5],将会返回3,与题目要求不符。
auto t = q.front();
q.pop();
if(!t->left &&!t->right) return sum;
if(t->left) q.push(t->left);
if(t->right) q.push(t->right);
}
}
return ;
}
};

DFS:

C++代码:

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
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));
}
};

(二叉树 BFS DFS) leetcode 111. Minimum Depth of Binary Tree的更多相关文章

  1. &lpar;二叉树 BFS DFS&rpar; leetcode 104&period; Maximum Depth of Binary Tree

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

  2. &lbrack;LeetCode&rsqb; 111&period; 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. &lbrack;LeetCode&rsqb; 111&period; Minimum Depth of Binary Tree &star;&lpar;二叉树的最小深度&rpar;

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

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

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

  5. LeetCode 111&period; 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 111 Minimum Depth of Binary Tree&lpar;DFS&rpar;

    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

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

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

  9. Java &lbrack;Leetcode 111&rsqb;Minimum Depth of Binary Tree

    题目描述: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along th ...

随机推荐

  1. iOS沙盒简单介绍

    先简单介绍一下什么是沙盒:你可以简单理解成为一个目录,这个目录的改动不会对操作系统造成任何损失.(这里也有一点点介绍) 看看苹果的沙盒目录: 再附一张苹果官方的图 一个iOS app操作都是在自己的沙 ...

  2. 我在 impress&period;js 中学到的小套路

    我在 impress.js 中学到的小套路 写在开篇 作为了一个自学 JavaScript 才一个月的新手,前几天“妄图”研究 jQuery-3.1.0 源码,结果自然是被虐得死去活来.机缘巧合之下, ...

  3. C&plus;&plus;11多元组类别

    [C++11多元组类别] 多元组可被视为是 struct 其数据成员的一般化.底下是一个多元组类别的定义和使用情况: 我们可以定义一个多元组类别对象 proof 而不指定其内容,前提是 proof 里 ...

  4. Delphi 串口打印机打印

    一.硬件准备 打印机: 打印机必须具有串口,没有标配串口的打印机,必须购买串口卡,串口卡的型号请参考随机<操作手册>. 计算机: 计算机必须具有串口,计算机通常具有两个串口:COM1和CO ...

  5. android 利用Bitmap获取圆角矩形、圆形图片

    1.在很多时候,我们要显示图片资源,需要将他的资源显示为圆角的:示例源码如下: public static Bitmap getRoundedCornerBitmap(Bitmap bitmap,fl ...

  6. Eclipse关联Java源代码

    一个很简单的技巧,不多说,直接贴图 1. 2 . 3.选择你jdk下的src.zip就可以了.搞定!

  7. Unity3D项目程序加密-VirboxProtector加壳工具

    各位Unity3D的开发者,你还为你的代码被反编译而头疼, 混淆和加密已经失效,为内存dump代码而烦恼?是否辛苦制作的游戏被盗版被抄袭而烦恼? 是否害怕算法被别人参考要把算法写成C++而费劲周折? ...

  8. 浏览器开发者工具----F12 功能介绍

    笔者技巧: 看了些其它回答,有些是用来扒图片的,有些是写爬虫的(这个不要看Elements,因为浏览器会对一些不符合规范的标签做补全或者其它处理,最好是Ctrl+U). 图片的话就不要看Network ...

  9. 解决镜像无法删除的问题multiple repositories

    Error response from daemon: conflict: unable to delete ea5f89e79b1e (must be forced) - image is refe ...

  10. 【转】DHCP工作过程详解

    DHCP动态主机配置协议的作用我想作为网管的兄弟们都应该知道了,这里我就不多废话了,今天我要谈的是DHCP的工作过程,了解了工作过程,要排除故障就容易了.   一.DHCP客户机初始化: 1. 寻找D ...