Leetcode:Minimus Depth of Binary Tree
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的更多相关文章
- 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 ... 
- LeetCode——Maximum Depth of Binary Tree
		LeetCode--Maximum Depth of Binary Tree Question Given a binary tree, find its maximum depth. The max ... 
- [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 ... 
- [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 ... 
- 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 ... 
- LeetCode - Minimum Depth of Binary Tree
		题目: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the ... 
- 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 ... 
- [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 ... 
- [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 ... 
随机推荐
- xml:Invalid byte 2 of 2-byte UTF-8 sequence
			xml解析错误:Invalid byte 2 of 2-byte UTF-8 sequence 在做接口解析时候出现的错误:Invalid byte 2 of 2-byte UTF-8 sequenc ... 
- CSharp设计模式读书笔记(22):策略模式(学习难度:★☆☆☆☆,使用频率:★★★★☆)
			策略模式(Strategy Pattern):定义一系列算法类,将每一个算法封装起来,并让它们可以相互替换,策略模式让算法独立于使用它的客户而变化,也称为政策模式(Policy). 模式角色与结构: ... 
- 【百度地图API】如何使用suggestion--下拉列表方式的搜索建议
			原文:[百度地图API]如何使用suggestion--下拉列表方式的搜索建议 摘要: 百度地图上有一个很强大的搜索建议功能,以下拉列表的方式展示出来.比如,输入“百度”,下拉列表中就会出现“北京市海 ... 
- Android 从硬件到应用程序:一步一步爬上去 6 -- 我写的APP测试框架层硬件服务(终点)
			创Android Applicationproject:采用Eclipse的Android插入ADT创Androidproject,project名字Gpio,创建完成后,project文件夹pack ... 
- HDU1325 Is It A Tree? 【并查集】
			Is It A Tree? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ... 
- MVC使用百度开源文本编辑器UEditor实现图文并茂,字数限制,上传图片或涂鸦
			原文:MVC使用百度开源文本编辑器UEditor实现图文并茂,字数限制,上传图片或涂鸦 文本编辑器有很多,比如ticymce和CKEditor就比较好用,但涉及到图片.文件上传,需要结合CKFinde ... 
- Web API 2 对 CORS 的支持
			Web API 2 对 CORS 的支持 CORS概念 跨域资源共享 (CORS) 是一种万维网联合会 (W3C) 规范(通常被认为是 HTML5 的一部分),它可让 JavaScript 克服由浏览 ... 
- ASP.NET 5是如何通过XRE实现跨平台的
			挡不住的好奇心:ASP.NET 5是如何通过XRE实现跨平台的 .NET程序员也有自己的幸福,.NET的跨平台是一种幸福,.NET的开源也是一种幸福,而更幸福的是可以通过开源的.NET了解.NET ... 
- js 正则之检测素数
			原文:js 正则之检测素数 相信很多人应该看过这篇文章,我第一次看到的时候是11年的样子,那时候学vbs的时候看过这个问题.原文<检查素数的正则表达式>,在文章里已经解释了他是怎么判断的, ... 
- 第3章1节《MonkeyRunner源码剖析》脚本编写示例: MonkeyRunner API使用示例(原创)
			天地会珠海分舵注:本来这一系列是准备出一本书的,详情请见早前博文“寻求合作伙伴编写<深入理解 MonkeyRunner>书籍“.但因为诸多原因,没有如愿.所以这里把草稿分享出来,所以错误在 ... 
