非常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的更多相关文章

  1. leetcode第一刷_Maximum Depth of Binary Tree

    这道题预计是ac率最高的一道了.你当然能够用层序遍历,我佩服你的耐心和勇气.由于看到别人的三行代码,会不会流眼泪呢.. class Solution { public: int maxDepth(Tr ...

  2. 【一天一道LeetCode】#104. Maximum Depth of Binary Tree

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源:http ...

  3. 【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 ...

  4. 【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 ...

  5. LeetCode My Solution: Minimum Depth of Binary Tree

    Minimum Depth of Binary Tree Total Accepted: 24760 Total Submissions: 83665My Submissions Given a bi ...

  6. 【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 ...

  7. 【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 ...

  8. 【LeetCode OJ】Minimum Depth of Binary Tree

    Problem Link: http://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ To find the minimum dept ...

  9. 【一天一道LeetCode】#111. Minimum Depth of Binary Tree

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

随机推荐

  1. 昂贵的聘礼(Dijkstra)

    http://poj.org/problem?id=1062 每个物品看成一个节点,酋长的允诺也看作一个物品, 如果一个物品加上金币可以交换另一个物品,则这两个节点之间有边,权值为金币数,求第一个节点 ...

  2. [Apple开发者帐户帮助]四、管理密钥(2)获取密钥标识符

    创建JSON Web令牌(JWT)以与启用的服务进行通信时,需要密钥标识符. 获取密钥标识符 在“ 证书,标识符和配置文件”中,选择侧栏中“键”下的“全部”. 在右侧,选择私钥. 密钥标识符显示在密钥 ...

  3. Redis的事务讲解

    1. Redis事务的概念 是什么: 可以一次执行多个命令,本质是一组命令的集合.一个事务中的所有命令都会序列化,按顺序串行化的执行而不会被其他命令插入 能干嘛:一个队列中,一次性.顺序性.排他性的执 ...

  4. 2015 多校赛 第三场 1002 (hdu 5317)

    Description Mr. Hdu is interested in Greatest Common Divisor (GCD). He wants to find more and more i ...

  5. HTML <!DOCTYPE>标签 各版本对应的标签是否有无

    参考来源: http://www.w3school.com.cn/tags/html_ref_dtd.asp HTML5/HTML 4.01/XHTML 元素和有效的 DTD 下面的表格列出了所有的 ...

  6. vue 使用localStorage保存页面变量到浏览器变量中

    const STORAGE_KEY = 'todos-vuejs'//定义常量保存键值 export default{ fetch(){ return JSON.parse(window.localS ...

  7. 【Oracle】to_char技巧

    Select to_char(sysdate,'ss') from dual;      取当前时间秒部分 Select to_char(sysdate,'mi') from dual;      取 ...

  8. (转)Openlayers 2.X加载天地图

    http://blog.csdn.net/gisshixisheng/article/details/44621923 概述: 在前面的章节,讲到了Arcgis for js加载天地图,在本节讲述如何 ...

  9. java操作Excel的poi 设置单元格的对其方式

    设置单元格的对其方式 package com.java.poi; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.po ...

  10. [POI2005]SKA-Piggy Banks tarjan 水题

    Code: #include<bits/stdc++.h> #define maxn 1000002 using namespace std; void setIO(string s) { ...