题目:

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 (二)

    处理器图像: 处理后图像: 代码: from PIL import Image image = Image.open('4.jpg') image = image.convert('L') image ...

  2. CentOS 安装Mosquitto及测试

    系统信息,阿里云服务器 安装工具 yum install gcc gcc-c++ yum install openssl-devel yum install c-ares-devel yum inst ...

  3. linux命令分享一:压缩命令

    Linux操作系统中,*.zip.*.tar.*.tar.gz.*.tar.bz2.*.tar.xz.*.jar.*.7z等格式的压缩与解压 zip格式 压缩: zip -r [目标文件名].zip ...

  4. RecyclerView 作为聊天界面,被键盘遮挡的解决办法

    最近项目在重构,使用 RecyclerView 替换了 ListView 作为 IM 的聊天界面.然后遇到了一个问题就是当键盘弹出来的时候,键盘会遮挡住 RecyclerView 的一部分,造成聊天内 ...

  5. Android 调试利器:Stetho + Chrome

    简介 Stetho 由 Facebook 开发的一款查看 Android 数据库.SharePreference.网络拦截器的利器,通过与 Chrome 的配合使用,使 Android App 开发过 ...

  6. 分享:三种取消选中单选框radio的方法

    三种取消选中radio的方式,本文依赖于jQuery,其中第一种,第二种方式是使用jQuery实现的,第三种方式是基于JS和DOM实现的. <!DOCTYPE HTML> <html ...

  7. Atitit 项目管理 提升开发效率的项目流程方法模型 哑铃型  橄榄型 直板型

    Atitit 项目管理 提升开发效率的项目流程方法模型 哑铃型  橄榄型 直板型 项目主体三个部分 ui界面,中间层,数据库 按照不同的比重可以分为一下三个模型  哑铃型  橄榄型 直板型 哑铃型 开 ...

  8. Atitit nodejs5 nodejs6  nodejs 7.2.1  新特性attialx总结

    Atitit nodejs5 nodejs6  nodejs 7.2.1  新特性attialx总结 1.1. Node.js 4.0.0 已经发布了 .这是和 io.js 合并之后的首个稳定版本,它 ...

  9. 绕过chrome的弹窗拦截机制

    在chrome的安全机制里面,非用户触发的window.open方法,是会被拦截的.举个例子: var btn = $('#btn'); btn.click(function () { //不会被拦截 ...

  10. lua--clone

    clone 深度克隆一个值. 格式: value = clone(值) 用法示例: -- 下面的代码,t2 是 t1 的引用,修改 t2 的属性时,t1 的内容也会发生变化 , b = } local ...