非常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. springboot踩坑出坑记

    4月15到4月17我都在把毕设从eclipse重构到IDEA中,springboot最让我头疼的是它的版本问题,因为每一个版本对应的依赖包都有可能出错,这里分享一下如何成功移植用eclipse写的sp ...

  2. Android插件化原理解析——Hook机制之动态代理

    转自 http://weishu.me/2016/01/28/understand-plugin-framework-proxy-hook/ 使用代理机制进行API Hook进而达到方法增强是框架的常 ...

  3. java 实现将java对象转为yaml文件

    首先我们建两个类,以下两个类展示的是一个学生拥有多个手机号码联系人. 先是学生类: package com.ming.yaml.beans; import java.util.ArrayList; i ...

  4. 3.sql基础

    sql语句是和dbms交谈专用的语句,不同dbms都认sql语法 sql语句中字符串用单引号 sql语句是大小写不敏感的,不敏感指的是sql关键字,字符串值还是大小写敏感的 创建表.删除表不仅可以手工 ...

  5. Get 和 Post

    理论: Http定义了与服务器交互的不同方法,最基本的方法有4种,分别是GET,POST,PUT,DELETE.URL全称是资源描述符,我们可以这样认为:一个URL地址,它用于描述一个网络上的资源,而 ...

  6. Android RecyclerView初体验

    很早之前就听说过RecyclerView这个组件了,但一直很忙没时间学习.趁着周末,就花了一天时间来学习RecyclerView. 准备工作 在Android Studio里新建一个Android项目 ...

  7. NuGet 下载dll

    PM> Install-Package DotNetCore.CAP PM> Install-Package  xxxdll

  8. servlet-后台获取form表单传的参数

    前台代码: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> & ...

  9. more-less-cat-tail-head 命令简单分析

    区别:cat一次性把文件内容全部显示出来,管你看不看得清,显示完了cat命令就返回了,不能进行交互式 操作,适合察看内容短小.不超过一屏的文件:more比cat强大一点,支持分页显示,你可以ctrl+ ...

  10. ubuntu 16.04 安装QT问题

    使用 sudo  sh ./**.run  有错误: 增加 文件的可运行权限: sudo chmod +x Qt.run 直接运行: ./Qt.run 可完成安装