leetcode第一刷_Minimum Depth of Binary Tree
非常easy的题目。只是还是认为要说一下。
最小深度。非常快想到bfs,层序遍历嘛。本科的时候实在是没写过多少代码,一開始竟然想不到怎么保存一层的信息。后来想到能够压入一个特殊的对象,每次到达这个对象就知道是一层了。我用的是空指针。认为这个适用性还是不错的。一层的节点入队结束后,应该压入一个NULL。当一层的节点都处理完。遇到NULL的时候,要在队列尾部再入队一个NULL,这是后一层的分界线嘛。
昨天在还有一道题上看到了还有一种做法。用一个数据结构vector<set<*> >(2)。当然vector里面包裹的是什么结构体并不重要,仅仅要能够高速的压入和顺序訪问就可以。vector的维度是二维的,保存当前层和上一层的对象。然后用两个相互排斥int值cur和pre来保存正在訪问和上一次訪问的vector,每一次遍历,扫描pre层。发现的节点增加到cur层,下次循环时,交换一下。
我认为这是一种非常好的思路,尽管用在普通的层序遍历上有杀鸡用牛刀了。
class Solution {
public:
int minDepth(TreeNode *root) {
if(root == NULL)
return 0;
int res = 1;
queue<TreeNode*> ceng;
TreeNode *pNode;
ceng.push(root);
ceng.push(NULL);
while(!ceng.empty()){
if(ceng.front() == NULL){
res++;
ceng.pop();
ceng.push(NULL);
}
pNode = ceng.front();
ceng.pop();
if(!pNode->left&&!pNode->right)
return res;
if(pNode->left)
ceng.push(pNode->left);
if(pNode->right)
ceng.push(pNode->right);
}
return res;
}
};
leetcode第一刷_Minimum Depth of Binary Tree的更多相关文章
- leetcode第一刷_Maximum Depth of Binary Tree
这道题预计是ac率最高的一道了.你当然能够用层序遍历,我佩服你的耐心和勇气.由于看到别人的三行代码,会不会流眼泪呢.. class Solution { public: int maxDepth(Tr ...
- 【一天一道LeetCode】#104. Maximum Depth of Binary Tree
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源:http ...
- 【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练习题】Maximum Depth of Binary Tree
Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum depth is the n ...
- LeetCode My Solution: Minimum Depth of Binary Tree
Minimum Depth of Binary Tree Total Accepted: 24760 Total Submissions: 83665My Submissions Given a bi ...
- 【LeetCode】104. Maximum Depth of Binary Tree (2 solutions)
Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum depth is the ...
- 【LeetCode】111. Minimum Depth of Binary Tree (2 solutions)
Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...
- 【LeetCode OJ】Minimum Depth of Binary Tree
Problem Link: http://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ To find the minimum dept ...
- 【一天一道LeetCode】#111. Minimum Depth of Binary Tree
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
随机推荐
- MYSQL 数据库命令行终端操作笔记
1.数据库登录: 1.登录本地的MYSQL数据库:mysql -u root -p 2.连接远程主机上的MYSQL数据库:mysql -h 192.168.191.2 -u root -p 123 ...
- 使用javac编译java文件
过程中遇到的几个问题记录如下: 1.java -version正常显示java版本,但是javac却显示[不是内部外部命令] 原因:JAVA_HOME设置成了用户环境变量,Path里用%JAVA_HO ...
- 网络流模板(模板题:POJ1273)
模板题:POJ1273 EK: #include <queue> #include <cstdio> #include <cstring> #include < ...
- Spring Boot (17) 发送邮件
添加依赖 <!--发送邮件 --> <dependency> <groupId>org.springframework.boot</groupId> & ...
- Visual Studio切换界面显示语言
[工具]-[选项]-[环境]-[区域设置]-[语言]-[获取其他语言] 安装后重启即可.
- 5.13redis的相关基础
二.Redis(NoSql) Redis是用C语言开发的一个开源的高性能键值对(key-value)数据库,官方提供测试数据,50个并发执行 100000个请求,读的速度是110000次/s,写的速 ...
- C# 多线程系列(二)
传递数据给一个线程 通过函数或lambda表达式包一层进行传递. static void Main(string[] args) { Thread thread = new Thread(() =&g ...
- iOS网络——NSURLCache设置网络请求缓存
今天在看HTTP协议,看到了response头中的cache-control,于是就深入的研究了一下.发现了iOS中一个一直被我忽略的类——NSURLCache类. NSURLCache NSURLC ...
- DataTable如何去除重复的行
两种方法1 数据库直接去除重复select distinct * from 表名去除了重复行distinct 2 对 DataTable直接进行操作DataTable dt=db.GetDt(&quo ...
- windows常用的cmd命令和常用操作。
这几日部署Jenkins,牵扯到很多东西,比如用到许多cmd命令和Linux命令.查找比较花时间,因此将查看的文档留下,以避免下次重新查找浪费时间. Windows cmd命令: http://blo ...