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.

int minDepth(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(root == NULL) return 0;
queue<TreeNode*> que;
que.push(root);
int count = 1;
int depth = 0;
while(!que.empty())
{
TreeNode* tmp = que.front();
que.pop();
count--; if(tmp->left == NULL && tmp->right == NULL) break; if(tmp->left)
que.push(tmp->left);
if(tmp->right)
que.push(tmp->right); if(count == 0){
depth++;
count = que.size();
}
}
return ++depth;
}

leetcode_question_111 Minimum Depth of Binary Tree的更多相关文章

  1. [Leetcode][JAVA] Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree

    Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...

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

  3. 33. Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree

    Minimum Depth of Binary Tree OJ: https://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ Give ...

  4. 【LeetCode练习题】Minimum Depth of Binary Tree

    Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...

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

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

  6. LeetCode My Solution: Minimum Depth of Binary Tree

    Minimum Depth of Binary Tree Total Accepted: 24760 Total Submissions: 83665My Submissions Given a bi ...

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

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

  8. LeetCode: Minimum Depth of Binary Tree 解题报告

    Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...

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

随机推荐

  1. C语言基础05

    二维数组的定义: 数据类型 数组名称 [ 常量表达式1 ] [ 常量表达式2 ] = {.....} int a[ 2 ][ 3 ] ={ {4,5,6}, {7,8,0},   //或者{7} 后面 ...

  2. js 异步流程控制之 avQ(avril.queue)

    废话前言 写了多年的js,遇到过最蛋疼的事情莫过于callback hell, 相信大家也感同身受. 业界许多大大也为此提出了很多不错的解决方案,我所了解的主要有: 朴灵 event proxy, 简 ...

  3. Docker学习笔记之一,搭建一个JAVA Tomcat运行环境

    Docker学习笔记之一,搭建一个JAVA Tomcat运行环境 前言 Docker旨在提供一种应用程序的自动化部署解决方案,在 Linux 系统上迅速创建一个容器(轻量级虚拟机)并部署和运行应用程序 ...

  4. cdecl、pascal、stdcall、fastcall

    Directive Parameter order   Clean-up Passes parameters in registers?register   Left-to-right         ...

  5. Buns(dp+多重背包)

    C. Buns time limit per test 2 seconds memory limit per test 256 megabytes input standard input outpu ...

  6. OAuth2.0服务器端的实现

    authorize 授权关系存储表字段 备注appid 应用IDuserid 用户IDaddtime 添加时间…… 其他表3 access_token 访问令牌存储表字段 备注access_token ...

  7. mysql启动报错:Fatal error: Can’t open and lock privilege tables: Table ‘mysql.host’ doesn’t exist

    mysql在首次启动的时候可能会报错:Can’t open and lock privilege tables: Table ‘mysql.host’ doesn’t exist 这时候可以执行脚本 ...

  8. java中获取类加载路径和项目根路径的5种方法

    import java.io.File; import java.io.IOException; import java.net.URL; public class MyUrlDemo { publi ...

  9. oracle权限的分配

    一.创建 sys:   //系统管理员,拥有最高权限 system://本地管理员,次高权限 scott: //普通用户,密码默认为tiger,默认未解锁 二.登陆 sqlplus / as sysd ...

  10. 浅谈:SAMBA配置设置

      通过以下命令安装samba: yum install -y samba samba拥有三个服务,分别是: smbd 提供文件及打印共享功能,使用139.445端口 nmbd 提供NetBIOS支持 ...