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的更多相关文章

  1. [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)

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

  2. 【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 ...

  3. [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 ...

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

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

  5. leetcode 111 minimum depth of binary tree

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

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

  7. (二叉树 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 ...

  8. [LeetCode]题解(python):111 Minimum Depth of Binary Tree

    题目来源 https://leetcode.com/problems/minimum-depth-of-binary-tree/ Given a binary tree, find its minim ...

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

随机推荐

  1. python运行时禁止生成pyc文件

    方法 在环境变量文件~/.bashrc中添加 export PYTHONDONTWRITEBYTECODE=False source ~/.bashrc加载即可 如何从项目中删除所有.pyc文件 fi ...

  2. Gym 102082B : Arithmetic Progressions

    戳这里看题目吧! 题目大意:给你一个长度为n(2<=n<=5000)的序列,没有重复的数字,序列中每个元素满足(0<=numi<=1e9).求一个最长的数列满足差分值相同(除n ...

  3. Linux执行Java文件

    最近学习shell脚本,写个简单java类让linux去执行 java类没别的东西,就引了一个fastjson的jar,写了个main方法 序列化一个User对象 打印 package com.lws ...

  4. jmeter Thread Groups的顺序执行与并行执行

    本期目标: 理解Thread Groups的顺序执行与并行执行 控制因子:Run Thread Groups consecutively(i.e.one at time) 预期结论: 1.勾选 Run ...

  5. linux下又一次定位svn url方法

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/oudahe/article/details/34437661 linux下又一次定位svn url方 ...

  6. grep中正则表达式使用尖括号表示一个单词

    比如 grep '\<bin\>' /etc/passwd --color

  7. Install ncurses (ncurses-devel) and try again

    apt install libncurses5-dev libncursesw5-dev

  8. Linux (ifconfig/docker) 移除网桥/虚拟网卡

    今天上大数据实践课时,使用学校提供的云主机平台创建了几台vps,但是安全组配置好之后发现无法用ssh无法登录,ping也不通,提示网络无法到达. 但是拿别人的电脑试了下能顺利使用ssh连接. 有人说是 ...

  9. 删除DataFrame中特定条件的行/列

    在<Python进行数据分析与挖掘实战>一书中,第10章 删除热水器不工作的数据(水流量为0并且开关机状态为“关”的数据.) import pandas as pd data=pd.rea ...

  10. HDU-6532 Chessboard 2019广东省省赛B题(费用流)

    比赛场上很容易想到是费用流,但是没有想到建图方法qwq,太弱了. 这里直接贴官方题解: 费用流.离散化坐标,每行用一个点表示,每列也用一个点表示.表示第i-1行的点向表示第i行的点连边,容量为第i行及 ...