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 ...
随机推荐
- WCF中的数据契约(DataContract)
服务契约定义了远程访问对象和可供调用的方法,数据契约则是服务端和客户端之间要传送的自定义数据类型. 一旦声明一个类型为DataContract,那么该类型就可以被序列化在服务端和客户端之间传送,如下所 ...
- Oracle推断领域包括中国
假设你要推断领域包括中国.有一个简单的方法. SQL> drop table test purge; SQL> create table test as select * from dba ...
- Jquery页面中添加键盘按键事件,如ESC事件
$(document).keydown(function(event){ if(event.keyCode == 38 || event.keyCode == 104){ i--; if(i<= ...
- Silverlight中的Path
原文:Silverlight中的Path 在Silverlight中Path可能由直线.曲线.或者其他简单的图形对象组成.这篇文章旨在介绍如何使用XAML和C#来创建Path. 废话先行 Path可能 ...
- ftk学习记录(IME文章)
[声明:版权全部,欢迎转载.请勿用于商业用途. 联系信箱:feixiaoxing @163.com] 前面说的是全屏设置,还是请大家看一下效果图. watermark/2/text/aHR0cDovL ...
- [DevEpxress]GridControl 显示Gif动画
原文:[DevEpxress]GridControl 显示Gif动画 如果没有对进行设置,那么GridControl列中gif在编辑状态下,才能显示动画效果,如果要设置列自动显示动画效果,可以进行如下 ...
- PHP 13: 类
原文:PHP 13: 类 Notice: This article is working on progress!本章将介绍PHP类.现在,基本上每种语言都支持面向对象,当然PHP也不列外.PHP在以 ...
- HashMap之Hash碰撞冲突解决方案及未来改进
说明:参考网上的两篇文章做了简单的总结,以备后查(http://blogread.cn/it/article/7191?f=wb ,http://it.deepinmind.com/%E6%80%A ...
- 在VirtualBox下安装CentOS教程(截图版)
http://blog.csdn.net/kobe_lzq/article/details/7894718 使用的软件: VirtualBox 4.1.2 CentOS 5.6 x86_64 刻录的 ...
- openwrt构建过程探索
参考网站:http://wiki.openwrt.org/doc/howto/buildroot.exigence 需要下载必要的库文件,编译器等... 1 首先要获得openwrt的源码,参考ope ...