[leetcode]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.
分析:
要得出二叉树中全部从根到叶子节点路径中,经过结点最少路径上的节点数。
採用二叉树的层序遍历思想。每遍历一层。高度加一,仅仅要遇到叶子节点,就终止,并返回当前层数。
代码:
class Solution {
public:
int minDepth(TreeNode* root) {
deque<TreeNode*> store;
int left_num;
int res = 0;
if(!root)
return res;
store.push_back(root);
while(!store.empty()) {
res++;
vector<int> res_t;
left_num = store.size();
while(left_num-- > 0) {
const TreeNode* tmp = store.front();
if(!tmp->left&&!tmp->right)
return res;
store.pop_front();
res_t.push_back(tmp->val);
if(tmp->left)
store.push_back(tmp->left);
if(tmp->right)
store.push_back(tmp->right);
}
}
}
};
[leetcode]Minimum Depth of Binary Tree--二叉树层序遍历的应用的更多相关文章
- [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] 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 ☆(二叉树的最小深度)
[Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...
- 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】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] 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 111 Minimum Depth of Binary Tree 二叉树
找出最短的从叶子到根的路径长 可以回忆Maximum Depth of Binary Tree的写法,只不过在!root,我把它改成了10000000,还有max函数改成了min函数,最后的值如果是1 ...
随机推荐
- Maven:浅析依赖(dependency)关系中 scope 的含义(转)
在 Pom4 中,dependency 元素中引入了 scope 元素,这是一个很重要的属性.在Maven 项目中 Jar 包冲突.类型转换异常的很大原因是由于 scope 元素使用不当造成的. sc ...
- spring配置上传文件大小
上传文件过大时,不会进入控制层,会直接抛出异常,提示上传文件过大,如下: org.apache.commons.fileupload.FileUploadBase$SizeLimitExceededE ...
- mysqlslap 压力测试工具
[背景] 由于一些不可描述的原因,我要确定一条业务SQL在给定的MySQL环境下的执行效率:比如说200个session同时跑同样一条SQL 我们数据库的吞吐量怎么样? 刚收到这个需求的时候,感觉这个 ...
- Starting with neural network in matlab[zz]
转自:http://matlabbyexamples.blogspot.com/2011/03/starting-with-neural-network-in-matlab.html The neur ...
- [Kubernetes]Kubernetes的网络模型
Kubernetes的网络模型从内至外由四个部分组成: Pod内部容器所在的网络 Pod所在的网络 Pod和Service之间通信的网络 外界与Service之间通信的网络 建议在阅读本文之前先了解D ...
- struts2:图解action之HelloWorld示范(从action转到JSP)
虽然Struts 2.x的Action在技术上不需要实现任何接口或继承任何类型,但是,大多情况下我们都会出于方便的原因,使Action类继承com.opensymphony.xwork2.Action ...
- HDU 1431 素数回文
有人问我这个问题. 个人感觉暴搜会TLE O(n*sqrt(n)).n=100000000:(推断素数用2~sqrt(n)+1 去除) 还是枚举好了. 枚举 1~10000,把他每一位存下来,回文数已 ...
- iOS 应用程序目录结构
iOS应用程序采用的是沙盒原理设计,普及一下知识:沙盒是在受限的安全环境中运行应用程序的一种做法,这种做法是要限制授予应用程序的代码访问权限. 具体的特点有三点:1.每个应用程序都有自己的存储空间: ...
- python(56):正则表达式积累
来源:http://www.runoob.com/python/python-reg-expressions.html re.match函数 re.match 尝试从字符串的起始位置匹配一个模式,如果 ...
- iOS开发如何学习前端(2)
iOS开发如何学习前端(2) 上一篇成果如下. 实现的效果如下. 实现了一个横放的<ul>,也既iOS中的UITableView. 实现了当鼠标移动到列表中的某一个<li>,也 ...