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. mysql配置文件 /etc/my.cnf 详细解释

    basedir = path 使用给定目录作为根目录(安装目录). character-sets-dir = path 给出存放着字符集的目录. datadir = path 从给定目录读取数据库文件 ...

  2. 剥下“java.lang.OutOfMemoryError: unable to create new native thread”的外衣 创建线程数公式(MaxProcessMemory - JVMMemory – ReservedOsMemory)

    剥下“java.lang.OutOfMemoryError: unable to create new native thread”的外衣 星期一早上到了公司,据称产品环境抛出了最可爱的异常—OutO ...

  3. 如何用cacti监控windwos

    1:模版下载地址 https://github.com/mrlesmithjr/cacti resource \ snmp_queries 的文件放到cacti服务器对应的目录下 导入模版文件(在te ...

  4. Anti-Forgery Request Recipes For ASP.NET MVC And AJAX

    Background (Normal scenario of form submitting) To secure websites from cross-site request forgery ( ...

  5. 【java】解析java中的数组

    目录结构: contents structure [+] 一维数组 1,什么是一维数组 2,声明一维数组的三种方式 二维数组 1,什么是二维数组 2,声明二维数组的3种方式 3,二维数组的遍历示例 数 ...

  6. k近邻算法-java实现

    最近在看<机器学习实战>这本书,因为自己本身很想深入的了解机器学习算法,加之想学python,就在朋友的推荐之下选择了这本书进行学习. 一 . K-近邻算法(KNN)概述 最简单最初级的分 ...

  7. ASP.NET实现二维码 ASP.Net上传文件 SQL基础语法 C# 动态创建数据库三(MySQL) Net Core 实现谷歌翻译ApI 免费版 C#发布和调试WebService ajax调用WebService实现数据库操作 C# 实体类转json数据过滤掉字段为null的字段

    ASP.NET实现二维码 using System;using System.Collections.Generic;using System.Drawing;using System.Linq;us ...

  8. SQLServer2008 全文检索摘记

    最近在做全文搜索的内容,google了一下全文检索,发现了一些问题,现在总结如下: 全文索引和查询概念(摘自SQL 联机帮助)SQL Server 2008 为应用程序和用户提供了对 SQL Serv ...

  9. samba 服务器搭建

    为了能在两台机器上共享代码,方便测试不同平台性能和搭建分布式的web server,今天耗费半天时间搭建一个samba服务器共享数据,要求开放写权限,但多次实验均告失败,最终在 鸟哥 的提醒下 检查发 ...

  10. windows 中安装及使用 SSH Key

    转自 简书技术博客:https://www.jianshu.com/p/a3b4f61d4747 联系管理员开通ssh功能: 重新创建环境: 下载工具包到本地机器wsCli 0.4 解压后,把相应的w ...