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. Windows移动开发(一)——登堂入室

    開始本博客之前先分享一个自己的好消息吧,2014年3月31日起,正式就职于北京****集团Win8project师.主要负责将IOS和Android应用移植到Win8.1平板上,目标客户是银行,闲话不 ...

  2. POJ1201-Intervals(差动限制)

    Intervals Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 20786   Accepted: 7866 Descri ...

  3. linux_操作基本语句

    总结一下常用的和不常用的linux命令,有些命令不常用的,是要反复去看才能记住的. 1.最基础的ls命令,相当于win下的dir命令,常用参数有 -a,-l 2.cd命令,cd到一个目录,跟win下的 ...

  4. Swift构造函数(Initializer)和析构函数(Deinitializer)

    要初始化结构和类和其他类型的实例的属性. 默认的构造函数 struct Fahrenheit { var temperature: Doubleinit(){ temperature = 32.0 } ...

  5. 使用Visual Studio创建映像向导(Image Sprite)——Web Essential

    原版的:Creating Image Sprite in Visual Studio - Web Essential 译者注:有关图片精灵的信息请參阅http://baike.baidu.com/vi ...

  6. rpm安装FAQ

    1.error: cannot create %sourcedir /usr/src/redhat/SOURCES错误的解决方案 显现error: cannot create %sourcedir / ...

  7. 安卓Monkey源码分析之运行流程

    在<MonkeyRunner源码分析之与Android设备通讯方式>中,我们谈及到MonkeyRunner控制目标android设备有多种方法,其中之一就是在目标机器启动一个monkey服 ...

  8. JS判断字符串是否全为中文

    //第一种代码(全为中文则返回"true",不全为中文则返回"false"): <script language="javascript&quo ...

  9. linux下编译安装mysql5.5以上版本

    安装cmake: tar zxvf cmake-2.8.4.tar.gz cd cmake-2.8.4 ./configure  --prefix=/usr/local/cmake make & ...

  10. C#验证IP地址

    using System.Net; try { IPAddress a = IPAddress.Parse(输入的IP字符串); } catch (System.Exception ex) { Mes ...