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.

给定一棵二叉树,找到它的最小深度。最小深度是沿着从根节点到最近叶节点的最短路径的节点数量。

 class Solution {
public:
typedef TreeNode* tree;
int run(TreeNode *root) {
if(!root)
return ;
queue<tree> q;
q.push(root);
tree cur,last;
int level=;
last=q.back();
while(q.size()){
cur=q.front();
q.pop();
if(cur->left==NULL&&cur->right==NULL)
break;
if(cur->left!=NULL)
q.push(cur->left);
if(cur->right!=NULL)
q.push(cur->right);
if(last==cur){
level++;
last=q.back();
}
}
return level; }
};

minimum-depth-of-binary-tree——二叉树遍历、链表、广度优先的更多相关文章

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

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

  2. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  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] Minimum Depth of Binary Tree 二叉树的最小深度

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

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

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

  6. [Leetcode] The 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. [LeetCode] 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. 111 Minimum Depth of Binary Tree 二叉树的最小深度

    给定一个二叉树,找出其最小深度.最小深度是从根节点到最近叶节点的最短路径的节点数量.详见:https://leetcode.com/problems/minimum-depth-of-binary-t ...

  9. [LeetCode] Maximum Depth of Binary Tree 二叉树的最大深度

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  10. LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree

    LeetCode:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth ...

随机推荐

  1. MySQL之单表查询、多表查询

    一.单表查询: 单个表的查询方法及语法顺序需要通过实际例子来熟悉 先将表数据创建下: mysql> create database singe_t1; # 建个数据库singe_t1 Query ...

  2. DRF框架中的演变View

    import json from django.db import DatabaseError from django.http import HttpResponse from django.htt ...

  3. H.264分层结构与码流结构

    H.264分层结构 H.264编码器输出的Bit流中,每个Bit都隶属于某个句法元素.句法元素被组织成有层次的结构,分别描述各个层次的信息. 在H.264 中,句法元素共被组织成  序列.图像.片.宏 ...

  4. PAT Basic 1034

    1034 有理数四则运算 本题要求编写程序,计算2个有理数的和.差.积.商. 输入格式: 输入在一行中按照“a1/b1 a2/b2”的格式给出两个分数形式的有理数,其中分子和分母全是整型范围内的整数, ...

  5. 解决php7.3 configure: error: off_t undefined

    //解决该问题:php7.3 configure: error: off_t undefined; check your library configuration # 添加搜索路径到配置文件echo ...

  6. bootstap 折叠

    data-toggle="collapse" 添加到您想要展开或折叠的组件的链接上. href 或 data-target 属性添加到父组件,它的值是子组件的 id

  7. 五、docker配置镜像加速器之阿里云

    1 配置docker加速器 实在忍受不了pull的速度--------- 访问网址: https://dev.aliyun.com/search.html 点击管理中心: 根据操作稳定配置:

  8. [ZJOI2007]时态同步 (树形DP)

    题目描述 小 Q在电子工艺实习课上学习焊接电路板.一块电路板由若干个元件组成,我们不妨称之为节点,并将其用数字 1,2,3-.进行标号.电路板的各个节点由若干不相交的导线相连接,且对于电路板的任何两个 ...

  9. PHP中的验证码类(准备篇)

    <!--code.php内容--> <?php //开启session session_start(); include "vcode.class.php"; / ...

  10. SQL Server中的@@ROWCOUNT

    SQL Server中@@ROWCOUNT返回受上一语句影响的行数,返回值类型为 int 整型. 如果行数大于 20 亿,则需要使用 ROWCOUNT_BIG. @@ROWCOUNT和@@ERROR变 ...