力扣算法题—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 ...
随机推荐
- python requests函数封装方法
python requests函数封装方法 上代码 import requests import json """ 封装request请求, 1.post:my_pos ...
- AJAX请求,返回json进行页面绑值
AJAX请求,返回json进行页面绑值 后台 controller @RequestMapping(value = "backjson.do",method=RequestMeth ...
- Android 发布自动版本号方案
以前看到一些自动化版本号打包的文章.如果您的项目是用 Git 管理的,并且恰巧又是使用 Gradle 编译(应该绝大部分都是这样的了吧?),本文试图找到一种更加优雅的自动版本管理方法. 背景 我们都知 ...
- WPF常规表单验证
1:ViewModel 实现验证接口 IDataErrorInfo 2:实现接口的相关验证逻辑,并把错误信息反馈给 Error public string this[string columnName ...
- MyBatis笔记一:GettingStart
MyBatis笔记一:GettingStart 1.MyBatis优点 我们的工具和各种框架的作用就是为了我们操作数据库简洁,对于一些数据库的工具能帮我们少写一些处理异常等等的代码,但是他们并不是自动 ...
- Codeforces 351C Jeff and Brackets 矩阵优化DP
题意:你要在纸上画一个长度为n * m的括号序列,第i个位置画左括号的花费是a[i % n], 画右括号的花费是b[i % n],问画完这个括号序列的最小花费.n <= 20, m <= ...
- vue 使用Better-Scroll
注意点 1. 外层容器wrapper要设置高度,并且overflow:hidden. 2. wrapper里面的需要一个div包裹所有内容 3. 样式成功 4. 以上就是可以滚动的情况,wrappe ...
- Delphi如何获取一个字符串再另一个字符串中最后一次出现的位置
uses StrUtils; function ReversePos(SubStr, S: String): Integer; var i : Integer; begin i := Po ...
- css 一行内显示 超出自动隐藏
一般的文字截断(适用于内联与块): Example Source Code [www.mb5u.com].text-overflow {display:block;/*内联对象需加*/width:31 ...
- JDK1.8之后的新特性和新接口
接口的旧特性: 就特性下接口中只有: 常量(必须赋值) 抽象方法abstract(和final static private三个关键字冲突) interface Inter { //int a ; / ...