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.

SOLUTION 1:

递归

这种递归解法更简单。因为在本层递归中不需要考虑左右子树是否为NULL的情况。因为我们直接
把 null 设置为返回一个最大值,这样的话,如果出现空子树,它不会影响最小值。但是如果左
右均为空,则应返回1(即是仅仅为根节点)
        
而且这种做法更加合理。 因为如果是空树,应该是无法到达才是。这时就应该将值设置为最大。

 // SOLUTION 1:
public int minDepth1(TreeNode root) {
/*
主页君认为,在这应该是属于未定义行为,这里我们定义为MAX会比较好,因为
null就是取不到任何节点,没有path,不应该将最小值定为0.
*/
if (root == null) {
return 0;
} return dfs(root);
} /*
* The Recursion Version:
* 这种递归解法更简单。因为在本层递归中不需要考虑左右子树是否为NULL的情况。因为我们直接
把 null 设置为返回一个最大值,这样的话,如果出现空子树,它不会影响最小值。但是如果左
右均为空,则应返回1(即是仅仅为根节点) 而且这种做法更加合理。 因为如果是空树,应该是无法到达才是。这时就应该将值设置为最大。
* */
public int dfs(TreeNode root) {
if (root == null) {
return Integer.MAX_VALUE;
} // The base case: the root is a leaf.
if (root.left == null && root.right == null) {
return 1;
} return Math.min(dfs(root.left), dfs(root.right)) + 1;
}

SOLUTION 2:

使用level traversal会更快。因为我们要的是最短深度。当达到叶子节点 就可以直接退出了。

 // SOLUTION 2:
// Level Traversal:
public int minDepth(TreeNode root) {
/*
主页君认为,在这应该是属于未定义行为,这里我们定义为MAX会比较好,因为
null就是取不到任何节点,没有path,不应该将最小值定为0.
*/
if (root == null) {
return 0;
} int level = 0; Queue<TreeNode> q = new LinkedList<TreeNode>();
q.offer(root); while (!q.isEmpty()) {
int size = q.size();
level++;
for (int i = 0; i < size; i++) {
TreeNode cur = q.poll(); if (cur.left == null && cur.right == null) {
return level;
} if (cur.left != null) {
q.offer(cur.left);
} if (cur.right != null) {
q.offer(cur.right);
}
}
} return 0;
}

GITHUB:

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/tree/MinDepth_1218_2014.java

LeetCode: Minimum Depth of Binary Tree 解题报告的更多相关文章

  1. 【LeetCode】111. Minimum Depth of Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 [LeetCode] 题目地址 ...

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

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

  4. 【LeetCode】104. Maximum Depth of Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:BFS 方法二:DFS 参考资料 日期 题目 ...

  5. LeetCode 104 Maximum Depth of Binary Tree 解题报告

    题目要求 Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the ...

  6. 【LeetCode】655. Print Binary Tree 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...

  7. LeetCode - Minimum Depth of Binary Tree

    题目: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the ...

  8. [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 ...

  9. 【LeetCode】965. Univalued Binary Tree 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https://le ...

随机推荐

  1. apache的mod_proxy 反向代理

    在某台机器上(跑着apache)为某个域名(如www.selboo.com.cn)做反向代理正好知道apache有mod_proxy这个 东东于是看了看文档写下了如下的配置文件: <Virtua ...

  2. Nginx反向代理+Tomcat+Springmvc获取用户访问ip

    Nginx+Tomcat+Springmvc获取用户访问ip 1.Nginx反向代理 修改Nginx配置文件 location / { ***********之前代码*******; proxy_se ...

  3. Http网络协议

    目录结构: contents structure [-] 什么是HTTP协议 Http协议的发展历史 Http的报文结构 客户端请求 服务端响应消息 Content-Type application/ ...

  4. MyBatis---使用MyBatis Generator生成Dto、Dao、Mapping

    由于MyBatis属于一种半自动的ORM框架,所以主要的工作将是书写Mapping映射文件,但是由于手写映射文件很容易出错,所以查资料发现有现成的工具可以自动生成底层模型类.Dao接口类甚至Mappi ...

  5. GitHub Desktop 代码库管理工具

    1.GitHub Desktop 简介 GitHub Desktop 是用于 GitHub 项目版本控制软件. 官网下载地址 GitHub Desktop 其它下载地址 GitHub Desktop ...

  6. 那些令人喷饭的代码注释:仅以此代码献给...it's realy ?

    程序源代码中的注释经常是一个卧虎藏龙的地方,有人就很喜欢写幽默搞笑的注释内容.解释代码含义的同时,也带给人轻松神经的机会,确实是很有意思的风格,来看看这一辑国外某公司产品中的注释. 注意:看的时候严禁 ...

  7. MongoDB学习笔记(2)

    MongoDB 创建数据库 语法 MongoDB 创建数据库的语法格式如下: use DATABASE_NAME 如果数据库不存在,则创建数据库,否则切换到指定数据库. 实例 以下实例我们创建了数据库 ...

  8. Objective-C 如何让非等宽的数字和空格对齐

    在printf中,我们可以通过格式字符串来对文字进行对齐输出,比如: printf("%5d\n%5d", 12, 345); 在使用等宽字体的Console中,我们可以看到数字右 ...

  9. 10.翻译:EF基础系列---EF中的持久性

    原文链接:http://www.entityframeworktutorial.net/EntityFramework4.3/persistence-in-entity-framework.aspx ...

  10. python标准库介绍——35 pipes 模块详解

    ==pipes 模块== (只用于 Unix) ``pipes`` 模块提供了 "转换管道 (conversion pipelines)" 的支持. 你可以创建包含许多外部工具调用 ...