题目:

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. python os.chdir() 用法

    概述 os.chdir() 方法用于改变当前工作目录到指定的路径. 语法 chdir()方法语法格式如下: os.chdir(path) 参数 path -- 要切换到的新路径. 返回值 如果允许访问 ...

  2. OpenXml SDK 2.0 创建Word文档 添加页、段落、页眉和页脚

    using (WordprocessingDocument objWordDocument = WordprocessingDocument.Create(@"C:\********.doc ...

  3. IOS团队开发之——CocoaPods 第三方库管理工具

    使用前需要下载ruby 的gem 命令镜像,mac 下自带有.但一般不用,直接访问国外网站有限制. 下面安装 http://ruby.taobao.org/ http://blog.devtang.c ...

  4. JAXB--@XmlElementWrapper注解和泛型一起使用

    当java对象的某个属性使用泛型时,普通对象都没问题,但是遇到HashSet这种集合类封装的元素时,就会出现元素内容序列化不出来的问题,详见如下: 一.示例: 第一步:定义java对象 package ...

  5. gcp上使用gpu来学习tensorflow

    1080ti显卡实在是太贵了,8k一张的价格,让我感到无耐.还好,有gcp的gpu来训练,最有意思的是,他还提供300美元,让你挥霍. 1.当然是申请gcp的账号. 2.登录后,左侧->&quo ...

  6. Xilinx vivado迅雷下载地址(所有版本)

    注:其实该方法适用于提取Xilinx官网的任意工具的任意版本的迅雷下载地址 ①进入Xilinx官网,进入Device->Design Tools,选择你想要下载的任意工具.②进入新web页面,右 ...

  7. 利用Angular.js从PHP读取后台数据

    之前已经有非常多方法能够通过angular进行本地数据的读取.曾经的样例中,大多数情况都是将数据存放到模块的$scope变量中,或者直接利用ng-init定义初始化的数据. 可是这些方法都仅仅为了演示 ...

  8. HTML5学习笔记(十一):JavaScript基础

    JavaScript代码可以直接嵌在网页的任何地方,不过通常我们都把JavaScript代码放到<head>中: <head> <script> alert('He ...

  9. My new English

    作为一个程序员,不管你觉得是高端大气上档次,还是低调奢华有内涵.有一点不能否认,就是英语对于一个爱学习.想不断提高自己的程序员来说是非常重要的.不管是看英文文档,看英文的视频资料,还是想出国工作,与全 ...

  10. 每日英语:As World's Kids Get Fatter, Doctors Turn To The Knife

    Daifailluh al-Bugami was just a year old when his parents noticed that his lips turned blue as he sl ...