[Leetcode] The minimum depth of binary tree二叉树的最小深度
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.
方法一:
层次遍历。主体的思想不变,程序的主体结构也不变,具体遍历过程参照二叉树的层次遍历。关键在于终止条件,在某一层中,节点遍历的顺序是从左往右的,若是最短路径出现在该层中,则其中一定有某一节点的左、右孩子均不存在,即为叶节点。返回level+1是因为最小的深度要算该节点所在的层。
class Solution {
public:
int run(TreeNode *root)
{
if(root==NULL) return ;
int level=;
queue<TreeNode *> Q;
Q.push(root);
while( !Q.empty())
{
int levNum=;
int count=Q.size();
while(levNum<count)
{
TreeNode *temp=Q.front();
Q.pop();
if(temp->left==NULL&&temp->right==NULL)
return level+;
if(temp->left)
Q.push(temp->left);
if(temp->right)
Q.push(temp->right);
levNum++;
}
level++;
}
return level;
}
};
方法二:
思想还是层次遍历,写法不一样。make_pair中第一元素为节点,第二个元素为改节点所在的层数。即让每个进入队列中的节点都携带所在层数信息。非本人原创。
class Solution {
public:
int run(TreeNode *root)
{
queue<pair<TreeNode *,int>> Q;
if(root==NULL) return ;
Q.push(make_pair(root,));
while(!Q.empty())
{
pair<TreeNode *,int> cur=Q.front();
Q.pop();
if(cur.first->left==NULL&&cur.first->right==NULL)
return cur.second;
if(cur.first->left)
Q.push(make_pair(cur.first->left,cur.second+));
if(cur.first->right)
Q.push(make_pair(cur.first->right,cur.second+));
}
return ;
}
};
方法三:
递归算法,参见Grandyang:
class Solution {
public:
int minDepth(TreeNode *root) {
if (root == NULL) return ;
if (root->left == NULL && root->right == NULL) return ;
if (root->left == NULL) return minDepth(root->right) + ;
else if (root->right == NULL) return minDepth(root->left) + ;
else return + min(minDepth(root->left), minDepth(root->right));
}
};
递归的另一种写法:@牛客网友
class Solution {
public:
int run(TreeNode *root)
{
if(root==NULL) return ;
int lChild=run(root->left);
int rChild=run(root->right);
if(lChild==||rChild==)
return +lChild+rChild;
return +min(lChild+rChild);
}
};
[Leetcode] The minimum depth of binary tree二叉树的最小深度的更多相关文章
- [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)
[Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- [LeetCode] 111. 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] 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:111_Minimum Depth of Binary Tree | 二叉树的最小深度 | Easy
要求:此题正好和Maximum Depth of Binary Tree一题是相反的,即寻找二叉树的最小的深度值:从根节点到最近的叶子节点的距离. 结题思路:和找最大距离不同之处在于:找最小距离要注意 ...
- 111 Minimum Depth of Binary Tree 二叉树的最小深度
给定一个二叉树,找出其最小深度.最小深度是从根节点到最近叶节点的最短路径的节点数量.详见:https://leetcode.com/problems/minimum-depth-of-binary-t ...
- Leetcode 111 Minimum Depth of Binary Tree 二叉树
找出最短的从叶子到根的路径长 可以回忆Maximum Depth of Binary Tree的写法,只不过在!root,我把它改成了10000000,还有max函数改成了min函数,最后的值如果是1 ...
- LeetCode OJ Minimum Depth of Binary Tree 递归求解
题目URL:https://leetcode.com/problems/minimum-depth-of-binary-tree/ 111. Minimum Depth of Binary T ...
- [LeetCode] 104. Maximum Depth of Binary Tree 二叉树的最大深度
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
随机推荐
- 系统编程.py(多进程与多线程干货)
1.并发与并行* 多个任务轮换在CPU上跑叫并发* 多个任务在多个CPU上跑,没有交替执行的* 状态叫并行.通常情况下都是并发,即使是多核.* 而控制进程先执行谁后执行谁通过操作系统的调度算法.目前已 ...
- 数据解压及if else的应用
def sum(items): head, *tails = items return head + sum(tails) if tails else head # 最后一句有点像三目运算符,如果ta ...
- Hadoop(23)-Yarn资源调度器
Yarn是一个资源调度平台,负责为运算程序提供服务器运算资源,相当于一个分布式的操作系统平台,而MapReduce等运算程序则相当于运行于操作系统之上的应用程序 1. Yarn工作机制 机制详解 第1 ...
- STM32CubeMx配置USART注意的一个问题
HAL_UART_Receive_IT(&huart1, (uint8_t *)aRxBuffer, Number);意思是接收到Number个字节后,触发HAL_UART_RxCpltCal ...
- Python3爬虫(八) 数据存储之TXT、JSON、CSV
Infi-chu: http://www.cnblogs.com/Infi-chu/ TXT文本存储 TXT文本存储,方便,简单,几乎适用于任何平台.但是不利于检索. 1.举例: 使用requests ...
- Android面试收集录 电话、短信和联系人、多媒体技术
1.请写出调用系统拨号界面? Intent intent=new Intent(Intent.ACTION_DIAL,Uri.pase("tel:12345678910")); s ...
- 20145202马超 2006-2007-2 《Java程序设计》第3周学习总结
20145202马超 2016-2017-2 <Java程序设计>第3周学习总结 教材学习内容总结 函数的重载:在同一个类中,允许存在一个以上的同名函数,只要他们的参数数目不同就可以.与返 ...
- 图表制作工具之ECharts
简介 ECharts,缩写来自Enterprise Charts,商业级数据图表,一个纯Javascript的图表库,可以流畅的运行在PC和移动设备上,兼容当前绝大部分浏览器(IE6/7/8/9/10 ...
- ajax设置自定义头
一.setting参数 headers $.ajax({ headers: { Accept: "application/json; charset=utf-8" ...
- java集合浅谈(一)
一.类库结构图概览 容器对象仅能持有对象引用(对象的指针),而不是Copy对象信息,从网上搜得几张Java中集合类库的结构图,如下所示: 二.解说Collection 2.1 Collection ( ...