非常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. PCB MongoDB数据库 备份与还原

    一. MongoDB数据库 备份与还原工具介绍: 数据备份工具  mongodump.exe 数据还原工具   mongorestore.exe 二. MongoDB数据库备份 mongodump - ...

  2. 昂贵的聘礼(Dijkstra)

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

  3. 在PL/SQL中使用游标、动态sql和绑定变量的小例子

    需求:查询并输出30号部门的雇员信息 方式一:使用 loop...fetch SET serveroutput ON; DECLARE CURSOR c_emp IS ; v_emp emp%rowt ...

  4. 上传预览图片的插件jquery-fileupload

    上传预览图片的插件jquery-fileupload github地址:https://github.com/blueimp/jQuery-File-Upload 中文文档:http://www.jq ...

  5. page事件

    using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Secu ...

  6. 使用DOM解析XML文档

    简单介绍一下使用DOM解析XML文档,解析XML文件案例: <?xml version="1.0" encoding="UTF-8"?> -< ...

  7. 学习廖雪峰的Python教程之Python基础

    一.缩进 编译器或者解释器就是负责把符合语法的程序代码转换成CPU能够执行的机器码,然后执行. 以#开头的语句是注释,注释是给人看的,可以是任意内容,解释器会忽略掉注释.其他每一行都是一个语句,当语句 ...

  8. Type class-Typeclass-泛型基础上的二次抽象---随意多态

    对泛型的类型添加约束,从而使泛型类型的变量具有某种通用操作. 再使用这些操作,参与到其它操作中. In computer science, a type class is a type system  ...

  9. windows控制台(console)乱码

    在cmd中输入 CHCP 65001,实操有效.记录一下 永久设置,代码如下: Windows Registry Editor Version 5.00 [HKEY_CURRENT_USER\Cons ...

  10. html表单练习

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...