(二叉树 BFS DFS) 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 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时,只有这个结点的左子节点和右子节点同时为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的更多相关文章
- (二叉树 BFS DFS) leetcode 104. Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- [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 ...
- [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)
[Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...
- Leetcode 111 Minimum Depth of Binary Tree 二叉树
找出最短的从叶子到根的路径长 可以回忆Maximum Depth of Binary Tree的写法,只不过在!root,我把它改成了10000000,还有max函数改成了min函数,最后的值如果是1 ...
- 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 ...
- leetcode 111 Minimum Depth of Binary Tree(DFS)
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- leetcode 111 minimum depth of binary tree
problem description: Given a binary tree, find its minimum depth. The minimum depth is the number of ...
- 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 ...
- 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 ...
随机推荐
- 一、Dev
一.获取选中的表格 // MessageBox.Show(gridview_Parent.GetFocusedDataRow()["series"].ToString());//获 ...
- Microsoft Azure Machine Learning Studio
随着机器学习(ML)成为软件行业的主流,重要的是要了解它的工作原理,并将其置于开发栈中.了解如何为您的应用程序构建ML服务,您可以确定您的ML应用程序中的机会,实施ML,并与您的团队的ML专业人士清楚 ...
- Go语言类型的本质
如果给这个类型增加或者删除某个值,是要创建一个新值,还是要更改当前的值? 如果是要创建一个新值,该类型的方法就使用值接收者. 如果是要修改当前值,就使用指针接收者. 这个答案也会影响程序内部传递这个类 ...
- Codeforces734 E. Anton and Tree
传送门:>Here< 题意:给出一颗树,节点不是黑色就是白色,每次可以将一个颜色相同的块变颜色,问最少变几次才能让其变为同色 解题思路: 我们考虑由于每一次都是把同样颜色的色块进行变色,因 ...
- IOI2008 island
题目链接:[IOI2008]Island 题目大意:求基环树直径(由于题目的意思其实是类似于每个点只有一个出度,所以在每个联通块中点数和边数应该是相同的,这就是一棵基环树,所以题目给出的图就是一个基环 ...
- 百度分享不支持https的解决方案(单独部署静态文件)
首先是参考了博客,下载百度分享的静态代码 static 链接为:https://www.cnblogs.com/mmzuo-798/p/6434576.html 后来在nginx的 nginx.con ...
- MT【301】值域宽度
(2015浙江理科)已知函数$f(x)=x^2+ax+b,(a,b\in R)$.记$M(a,b)$是$|f(x)|$在区间$[-1,1]$上的最大值.(1)证明:当$|a|\ge2$时,$M(a,b ...
- python3 sys模块
模块sys有关python运行环境的变量和函数: 常用方法: sys.argv:一个列表,包含脚本外部传入的参数,argv[0]为脚本名 sys.exit([arg]):退出当前程序,可指定返回值或错 ...
- 持久化和公平分发.py
1.消息持久化在实际应用中,可能会发生消费者收到Queue中的消息,但没有处理完成就宕机(或出现其他意外)的情况,这种情况下就可能会导致消息丢失.为了避免这种情况发生,我们可以要求消费者在消费完消息后 ...
- bzoj2288 生日礼物 (线段树)
我当然想选最大的子段和啦 但要选M次 那不一定就是最好的 所以提供一个反悔的选项,我选了一段以后,就把它们乘个-1,然后再选最好的(类似于网络流的思路) 这个可以用线段树来维护,记一个区间包含左端点/ ...