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 ...
随机推荐
- 使用CNN(convolutional neural nets)关键的一点是检测到的面部教程(四):学习率,学习潜能,dropout
第七部分 让 学习率 和 学习潜能 随时间的变化 光训练就花了一个小时的时间.等结果并非一个令人心情愉快的事情.这一部分.我们将讨论将两个技巧结合让网络训练的更快! 直觉上的解决的方法是,開始训练时取 ...
- 手游开发者大会交流OGEngine新版本发布
由OGEngine第二个手游开发者交流会举办的圆桌会议在深圳市高新技术园区举行.发布会不仅吸引了手游开发商,供应商也纷纷在国外支付,在国内手游和国外出版商参加. 围绕三个主题会议讨论和交流.每个主题: ...
- eclipese with gdbserver and Jlink configuration
最近的项目,很感动于linux和gdbserver内容,I think must write something to record it . 各位对不起,图片挂了.. Ok, at first I' ...
- Roundabout for jQuery
效果图: Roundabout是一个转换静态HTML元素结构为交互式播放区域的jQuery插件(而且并不仅仅是一个转盘,还有许多的形状) 首先你要下载好Jquery.min.js,和Jquery-Ro ...
- centos7看电影
sudo rpm -Uvh https://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-5.noarch.rpm sudo rpm ...
- 随记一个C的时间加减
//Centos6 x86_64 #include <time.h>#include <stdio.h>#include <string.h>#include &l ...
- HtmlAgilityPack + Fizzler
HtmlAgilityPack + Fizzler 这两天在做个爬虫, 一次任务要下载3万多个页面, 然后从这3万多个页面提取数据. 以前写过两年的类似的东西, 基本都是写正则表达式, 速度快, 就是 ...
- Spring之使用Annotation注解开发项目
我们也可以使用Annotation来实现注入操作,提高我们写代码的灵活性和效率.spring中要使用annotation,需要在配置文件中增加: <beans xmlns="http: ...
- POJ 2114 Boatherds 划分树
标题效果:鉴于一棵树,问有两点之间没有距离是k的. 数据的多组 思维:和IOI2011的Race喜欢.不是这么简单.阅读恶心,我是在主要功能的别人的在线副本. CODE: #include <c ...
- Google Summer of Code 建议被接收的5个技巧
Google Summer of Code 建议被接收的5个技巧 本文翻译自:http://www.di.ens.fr/~baghdadi/TXT_blog/5_advices_to_get_your ...