The problem description:

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.

这个题目本身不是很难,二叉树的广搜找到最浅的那个子树。但这个题目是比较典型的再广搜的时候需要记录或者使用到当前深度信息,所以我们需要一层一层来处理,而不是

单纯的都放在队列里面,处理完为止。处理的时候,使用了2个队列,分别保存前后两层的节点。

 /**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int minDepth(TreeNode *root) {
if(root==NULL) return ;
int minDepth =;
queue<TreeNode*> outq,intq;
outq.push(root);
TreeNode* nd;
while(!outq.empty())
{
nd = outq.front();
outq.pop();
if(nd->left==NULL && nd->right ==NULL)
return minDepth;
if(nd->left!=NULL) intq.push(nd->left);
if(nd->right!=NULL) intq.push(nd->right); if(outq.empty())
{
swap(intq,outq);
minDepth++;
} }
return minDepth; }
};

注意,在处理的时候要保证没有空节点(NULL)被放进去了,不然可能导致外层outq在没有被内层赋值时,就被判断为空而跳出。

Leetcode:Minimus Depth of Binary Tree的更多相关文章

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

  2. LeetCode——Maximum Depth of Binary Tree

    LeetCode--Maximum Depth of Binary Tree Question Given a binary tree, find its maximum depth. The max ...

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

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

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

  6. LeetCode - Minimum Depth of Binary Tree

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

  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] Maximum depth of binary tree二叉树的最大深度

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

  9. [LeetCode] Maximum Depth of Binary Tree dfs,深度搜索

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

随机推荐

  1. 阅读《大数据》Tuzipeizhe

    一本好书.4/5明星. 内容:引进美国和信息,相关历史资料.从建国,为了连任奥巴马. 它是引入大型数据在美国,如何从头开始. 的流逝,到近期几年.这股影响美国的大数据 是怎样走入世界,影响各国的. 英 ...

  2. django csrf_token生成

    django模板中生成csrf_token的不同方式 系统环境 CENTOS 6.4 python 2.7.6 django 1.7.1 当post提交表单的的时候,是需要 csrf_token的, ...

  3. TS流文件

    简单介绍编辑 随着从HDTV录制的高清节目在网上的流传,烧友们对TS这个名词大概已经不陌生了.但随之而来就是怎样播放.怎样加入字幕等等的一系列问题.本文将重点介绍一下这方面的应用操作. 先来简要介绍一 ...

  4. nodeJS起步 1

    nodeJS起步 -- (1) 先来简单介绍nodeJS 我们知道JavaScript是运行在浏览器中的,浏览器为它提供了一个上下文(context),从而让JavaScript得以解析执行. nod ...

  5. NSString 常用分类

    #pragma mark 清空字符串中的空白字符 - (NSString *)trimString { return [self stringByTrimmingCharactersInSet:[NS ...

  6. Web API-属性路由

    路由(Routing)就是Web API如何将一个URI匹配到一个action的过程.Web API 2 支持一个新的路由方式-属性路由(attribute routing).顾名思义,属性路由使用标 ...

  7. ios 安装OpenFire

    1.开发xmpp官网下载 2.打开openfire.pkg 3.点击继续 4.成功安装后打开偏好设置 ->双击poenfire->弹出窗体[好] 5.随后会弹出以下这个视图 开启 strr ...

  8. Postman (Chrome插件)

    接口测试从未如此简单 - Postman (Chrome插件) Posted on 2015-01-16 15:50 WadeXu 阅读(468) 评论(7) 编辑 收藏 接口测试从未如此简单 - P ...

  9. Android在真机调试的设置方法

    1. 设置android手机为USB调试模式.步骤: menu---> 设置 ---> 应用程序 ---> 开发 , 选择[USB调试] 2. 用USB连接手机和电脑,并确保成功.步 ...

  10. FlexBuilder远程调试WEB应用

    Flex使用的开发工具FlexBuiler 3,web server使用apache. 1 第一次安装IE相应flashplayer的debug版本号,下载Flash player 11.8安装,下载 ...