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. iOS 开发中的 Tips(一)

    背景 学习6个小Tips 那就跟我一起学习小知识点吧.目录如下: 修改Mac终端(Terminal)里不同类型文件的显示颜色 修改Mac终端(Terminal)的提示文字 Mac终端显示/隐藏文件命令 ...

  2. Ant压缩与解压缩

    package com.test.utils; import java.io.File; import java.io.FileOutputStream; import java.io.InputSt ...

  3. Redis学习之路(007)- Redis学习手册(实例代码)

    在之前的博客中已经非常详细的介绍了Redis的各种操作命令.运行机制和服务器初始化参数配置.本篇博客是该系列博客中的最后一篇,在这里将给出基于Redis客户端组件访问并操作Redis服务器的代码示例. ...

  4. JS遍历Table的所有单元格内容

    用JS去遍历Table的所有单元格中的内容,可以用如下JS代码实现: 这个方法的参数是唯一标识Table的id,用document对象的获取. function GetInfoFromTable(ta ...

  5. mysql_install_db 运行结果

    # /usr/local/mysql/scripts/mysql_install_db \ > --defaults-file=/etc/my.cnf \ > --basedir=/usr ...

  6. 探索MVP(Model-View-Presenter)设计模式在SharePoint平台下的实现

    对于SharePoint Developers来说,往往会过多的去关注SharePoint平台和工具,而把设计模式和代码的可测试性放在了一个较低的优先级.这并不是说SharePoint Develop ...

  7. 【转载并整理】mysql分页方法

    http://blog.csdn.net/bestcleaner/article/details/52993468

  8. [转]IC行业的牛人

    转载的:   说来惭愧,我所了解的牛人也只是大学教授,工业界的高手了解的还太少,虽然我对教育界的牛人了解的也不多,但这里也要牢骚几句,论坛上的人好像只是认识Gray,Razavi,Allen,Lee, ...

  9. 转 阿里Dubbo疯狂更新,关Spring Cloud什么事?

    原文地址: http://www.ityouknow.com/springcloud/2017/11/20/dubbo-update-again.html阿里Dubbo疯狂更新,关Spring Clo ...

  10. Android 启动、绘制、显示过程

    Activity 启动过程: startActivity()-> Instrumentation.execStartActivity()-> Binder->ActivityMana ...