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 ...
随机推荐
- Ambari-部署文档
Ambari-server搭建过程 部署环境要求 操作系统:centos 5 或 centos 6 能够使用yum jdk版本号 1.7 官网安装教程 https://cwiki.apache.org ...
- shell编程控制结构:expr、let、for、while、until、shift、if、case、break、continue、功能、select
1.expr计算整数变量值 s=`expr 2 + 3` 运算符号和參数之间要有空格分开: 2.let命令 let s=(2+3)*4 echo $s 3.for语句 for 变量 in 列表 do ...
- BCM策略路由交换芯片
BCM几个交换芯片的寄存器和相关的路由 EGR_L3_NEXT_HOP.EGR_L3_INTF.ING_L3_NEXT_HOP BCM XGS系列SDK中和路由相关的几个命令 l3 l3table. ...
- Oracle OS认证和口令文件认证方法
OS认证 1.在SQLNET.ORA(位于$ORACLE_HOME/NETWORK/ADMIN文件夹中)文件里,使用vi编辑,凝视掉#SQLNET.AUTHENTICATION_SERVICES = ...
- 初识EPC
一.EPC定义 EPC=Event-driven Process Chain(事件驱动过程链) EPC建模方法最初由Keller, N¨uttgens和Scheer博士在1992年发表的Ereigni ...
- C语言编写Windows服务程序
原文:C语言编写Windows服务程序 #include <Windows.h> #include <stdio.h> #define SLEEP_TIME 5000 // 间 ...
- 关于WIN32.EXE变态木马下载器的解决办法
一.WIN32.EXE的来源:http://fdghewrtewrtyrew.biz/adv/130/win32.exe 二.运行后的表现:此WIN32.EXE通过80和8080端口访问若干个IP,若 ...
- UpdateModel方法
WebForm 对 MVC 说:能否借你的UpdateModel方法来用用? 背景 ASP.NET MVC的Controller有个很不错的方法:UpdataModel (相对应的还有TryUpdat ...
- 如何查找Linux的函数定义的位置?
网上的许多站点提供这样的服务,如下面这个: http://lxr.free-electrons.com/ident?v=3.10 Linux的错误返回值:3.10版本 Linux/include/ua ...
- CodeIgniter学习一:基础知识
1. url片段(CI域名组成说明) example.com/index.php/test/index 第一部分(test):控制器 第二部分(index):方法,动作 如果第二部分 ...