力扣算法题—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 ...
随机推荐
- (61)C# 可枚举类型和迭代器
一.可枚举类型 枚举器-Enumerator 是一个只读且只能在值序列向前移动的游标 枚举器需要实现下列接口之一 System.Collections.IEnumerator System.Coll ...
- intellij IDEA2016如何打可执行的jar包
尊重版权,原文地址:https://blog.csdn.net/liufeilong_sean/article/details/75254875 操作步骤: 1.在File -> project ...
- Web RTC录视频
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- vue-cli3.0打包完自动压缩zip
打包是我们常见的操作 一般打包完还需要压缩丢到服务器解压 首先 我们需要一个 filemanager-webpack-plugin npm i filemanager-webpack-plugin - ...
- python基础【第六篇】
list列表 基本结构 lst =[1,2,3,5,6] 为什么学列表? 列表能够存储比字符串更多的数据 列表能够存储各种数据类型 列表的注意点 列表是有序的 列表是可变的,支持索引,切片,步 切片后 ...
- spring security 学习二
doc:https://docs.spring.io/spring-security/site/docs/ 基于表单的认证(个性化认证流程): 一.自定义登录页面 1.在securityConfigy ...
- Spring学习笔记(9)——注入参数
集合类型属性 1.Set类型 private Set<String> sets=new HashSet<String>(); //我们需要给它添加set方法 public Se ...
- 2、eureka注册中心集群
1. Eureka作为spring cloud的服务发现与注册中心,在整个的微服务体系中,处于核心位置.单机模式下的eureka服务,显然不能满足高可用的实际生产环境,这就要求配置一个能够应对各种突发 ...
- 2018-2-13-git-cannot-lock-ref
title author date CreateTime categories git cannot lock ref lindexi 2018-2-13 17:23:3 +0800 2018-2-1 ...
- 2019-8-28-WPF-开发
title author date CreateTime categories WPF 开发 lindexi 2019-8-28 11:3:39 +0800 2018-2-13 17:23:3 +08 ...