题目:

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:
int minDepth(TreeNode* root) {
deque<TreeNode*> store; int left_num;
int res = 0;
if(!root)
return res;
store.push_back(root);
while(!store.empty()) {
res++;
vector<int> res_t;
left_num = store.size();
while(left_num-- > 0) {
const TreeNode* tmp = store.front();
if(!tmp->left&&!tmp->right)
return res;
store.pop_front();
res_t.push_back(tmp->val);
if(tmp->left)
store.push_back(tmp->left);
if(tmp->right)
store.push_back(tmp->right);
}
} }
};

[leetcode]Minimum Depth of Binary Tree--二叉树层序遍历的应用的更多相关文章

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

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

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

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

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

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

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

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

随机推荐

  1. ipod classic 检查硬盘方法

    长按(中间键+MENU)重启接着按(中键+左键)一直进入工程模式进入后顺序是:menu-IO-HARDDRIVE-HDSMARTDATA 就可以看到了. retracts--硬盘磁头非正常退回,比如硬 ...

  2. MySQL 死锁与日志二三事

    最近线上 MySQL 接连发生了几起数据异常,都是在凌晨爆发,由于业务场景属于典型的数据仓库型应用,白天压力较小无法复现.甚至有些异常还比较诡异,最后 root cause 分析颇费周折.那实际业务当 ...

  3. LeetCode Permutaions II

    LeetCode解题之Permutaions II 原题 输出一个有反复数字的数组的全排列. 注意点: 反复数字的可能导致反复的排列 样例: 输入: nums = [1, 2, 1] 输出: [[1, ...

  4. javascript 获取函数形参个数

    分享下javascript获取函数形参个数的方法. /** * 获取函数的形参个数 * @param {Function} func [要获取的函数] * @return {*} [形参的数组或und ...

  5. [fixed] 解决 slf4j + log4j eclipse 可以打印日志,而在云服务器上不能打印

    今天发现服务上没有打印任何日志,而log4j已经设置为了INFO 很奇怪,在eclipse中是可以打印的,也能输出到单独的日志中 后来发现原来是冲突了 把log4j注释掉即可 保留slf4j即可

  6. 【Unity】4.5 树木创建器

    分类:Unity.C#.VS2015 创建日期:2016-04-11 一.简介 在地形编辑器一节中,已经告诉了你如何使用已经创建好的树来形成大片树林.这一节告诉你在 Unity 5.3.4中如何利用[ ...

  7. stun服务器搭建(coTurn)

    0. 前言 好久之前写过一篇搭建WebRTC的文章,里面有简单的说到怎么搭建一个stun服务.但那时只是一笔带过.正好,这两天搭建stun服务,这篇博客就再复习一遍,并把搭建过程整理一下. 1. 安装 ...

  8. markModified声明要修改的数组字段

    更新一个文档的字段的时候,如果该字段的类型是数组类型,则必须在更新保存前声明一下这个数组字段要被修改,否则这个数组字段的值不会被修改.如 article.markModified('categorys ...

  9. c++map的用法

    Map是c++的一个标准容器,她提供了很好一对一的关系,在一些程序中建立一个map可以起到事半功倍的效果,总结了一些map基本简单实用的操作!1. map最基本的构造函数:   map<stri ...

  10. HTML中Zen Coding

    嵌套运算符 嵌套运算符用于以缩写的方式安排元素在生成文档树中的位置:将其放在内部或成为相邻的元素. 子: > 可以使用 > 运算符指定嵌套元素在另一个元素内部: div>ul> ...