力扣算法题—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.
Solution:
使用深度遍历和广度遍历
class Solution {
private:
int minLevel = INT32_MAX;
public:
int minDepth(TreeNode* root) {
if (root == nullptr)return ;
//return BFS(root);
int res = INT32_MAX;
DFS(root, , res);
return res;
} int BFS(TreeNode* root)
{
queue<TreeNode*>q;
q.push(root);
int level = ;
while (!q.empty())
{
queue<TreeNode*>temp;
++level;
while (!q.empty())
{
TreeNode* p = q.front();
q.pop();
if (p->left == nullptr && p->right == nullptr)return level;
if (p->left != nullptr)temp.push(p->left);
if (p->right != nullptr)temp.push(p->right);
}
q = temp;
}
return level;
}
void DFS(TreeNode* root, int level, int &res)
{
if (root->left == nullptr && root->right == nullptr) {
res = res > level ? level : res;
return;
}
if (root->left != nullptr)DFS(root->left, level + , res);
if (root->right != nullptr)DFS(root->right, level + , res);
} };
力扣算法题—111.Minimum Depth of Binary Tree的更多相关文章
- [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)
[Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...
- 【LeetCode】111. Minimum Depth of Binary Tree (2 solutions)
Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...
- [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 二叉树
找出最短的从叶子到根的路径长 可以回忆Maximum Depth of Binary Tree的写法,只不过在!root,我把它改成了10000000,还有max函数改成了min函数,最后的值如果是1 ...
- 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 (二叉树最小的深度)
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- (二叉树 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 shor ...
- [LeetCode]题解(python):111 Minimum Depth of Binary Tree
题目来源 https://leetcode.com/problems/minimum-depth-of-binary-tree/ Given a binary tree, find its minim ...
- 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 ...
随机推荐
- Missing artifact net.sf.json-lib:json-lib:jar:2.4
Missing artifact net.sf.json-lib:json-lib:jar:2.4 出现上述这种错误就是JAR没有引入进来 这时候发现是因为JDK版本的问题,所以需要在加一句 < ...
- java获取字符串编码和转换字符串编码
public class EncodingUtil { // 这里可以提供更多地编码格式,另外由于部分编码格式是一致的所以会返回 第一个匹配的编码格式 GBK 和 GB2312 public stat ...
- 使用soapui进行webservice接口测试
一.web service(SOAP)与HTTP接口的区别 1.什么是web service WebService就是Web服务的意思,对应的应用层协议为SOAP(相当于HTTP协议),可理解为远 ...
- java 并发——ReentrantLock
java 并发--ReentrantLock 简介 public class ReentrantLock implements Lock, java.io.Serializable { // 继承了 ...
- java中多态的要点
多态有三要素 继承.方法重写.父类引用指向子类对象 多态可以理解成两个部分:一个是编译时,一个是运行时 编译时:定义规则:运行时:具体实现这个规则. 编译时:把父类强制转换成子类不会报错:运行时:先去 ...
- python-前端JS
JavaScript JS(JavaScript)和 JAVA是没有任何关系的,使前端语言动作的基础 特点:现在对前端所有的动作操作都是基于JS操作的.是一门逻辑语言 简单易用:可以使用任何文本编辑工 ...
- pip安装任何包都出现问题
<!DOCTYPE html> { margin: 0; padding: 0; } body { background: url(images/body_bg.png) repeat-x ...
- 初探remoting双向通信(一)
原 初探remoting双向通信(一) 2013年06月24日 15:47:07 喜欢特别冷的冬天下着雪 阅读数 4389 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blo ...
- Python之字符串正则匹配
需求: 正则表达式匹配某个文本模式,但是它找到的是模式的最长可能匹配(因为是贪婪匹配 ). 而你想修改它变成查找最短的可能匹配. import re text2 = 'Computer says &q ...
- Android开发——回调(Callback)
1. 回调函数的定义: 在A类中定义了一个方法,这个方法中用到了一个接口和该接口中的抽象方法,但是抽象方法没有具体的实现,需要B类去实现,B类实现该方法后,它本身不会去调用该方法,而是传递给A类,供A ...