这是悦乐书的第168次更新,第170篇原创

01 看题和准备

今天介绍的是LeetCode算法题中Easy级别的第27题(顺位题号是111)。给定二叉树,找到它的最小深度。最小深度是沿从根节点到最近的叶节点的最短路径上的节点数。叶子节点是没有子节点的节点。例如:

给定二叉树[3,9,20,null,null,15,7],

       3/  \9   20/  \15   7

返回其最小深度= 2。

本次解题使用的开发工具是eclipse,jdk使用的版本是1.8,环境是win7 64位系统,使用Java语言编写和测试。

02 第一种解法

之前我们有解过最大深度的题,今天这道题相反,求最短路径,那是不是直接将原来代码中的最大改为最小即可?如果你这样试过,会发现根本不是那么回事!

特殊情况一:当传入的二叉树为空时,最短路径就是0。

特殊情况二:当传入的二叉树只有根节点时,最短路径是1.

正常情况:当某一节点的左子节点为空时,这时我们需要求其右子节点的最短路径;当某一节点的右子节点为空时,这时我们需要求其左子节点的最短路径;当某一节点的左子节点和右子节点都不为空时,这时我们要求其左子树和右子树的最短路径。

public int minDepth(TreeNode root) {if (root == null) {return 0;}if ((root.left == null) && (root.right == null)) {return 1;}if (root.left == null) {return minDepth(root.right) + 1;}if (root.right == null) {return minDepth(root.left) + 1;}return 1+Math.min(minDepth(root.left), minDepth(root.right));
}

03 第二种解法

除了上面的递归外,我们依旧可以使用遍历的方法。此解法与求最大深度时的第三种解法类似,也是利用队列,只是多了一步判断:当左右节点都为空时,此节点是叶子节点,需要更新最短路径的值。

public int minDepth2(TreeNode root) {if (root == null) {return 0;}Queue<TreeNode> queue = new LinkedList<>();queue.offer(root);int minDepth = Integer.MAX_VALUE;int depth = 1;while (!queue.isEmpty()) {int size = queue.size(); while (size > 0) {TreeNode t = queue.poll();if (t.left != null) {queue.offer(t.left);}if (t.right != null) {queue.offer(t.right);}if (t.left == null && t.right == null) {minDepth = Math.min(minDepth, depth); }size--;}depth++;}return minDepth;
}

04 小结

以上就是全部内容,如果大家有什么好的解法思路、建议或者其他问题,可以下方留言交流,点赞、留言、转发就是对我最大的回报和支持!

转载于:https://www.cnblogs.com/xiaochuan94/p/9943826.html

LeetCode算法题-Minimum Depth of Binary Tree(Java实现)相关推荐

  1. leetcode算法题--Minimum Depth of Binary Tree

    原题链接:https://leetcode.com/problems/minimum-depth-of-binary-tree/ int minDepth(TreeNode* root) {if (r ...

  2. LeetCode: 111. Minimum Depth of Binary Tree

    题目 Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bina ...

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

  4. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  5. leetcode - Minimum Depth of Binary Tree

    题目:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is th ...

  6. LeetCode刷题记录14——257. Binary Tree Paths(easy)

    LeetCode刷题记录14--257. Binary Tree Paths(easy) 目录 前言 题目 语言 思路 源码 后记 前言 数据结构感觉理论简单,实践起来很困难. 题目 给定一个二叉树, ...

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

  8. C#LeetCode刷题之#111-二叉树的最小深度​​​​​​​(Minimum Depth of Binary Tree)

    问题 给定一个二叉树,找出其最小深度. 最小深度是从根节点到最近叶子节点的最短路径上的节点数量. 说明: 叶子节点是指没有子节点的节点. 给定二叉树 [3,9,20,null,null,15,7], ...

  9. leetcode python3 简单题111. Minimum Depth of Binary Tree

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百一十一题 (1)题目 英文: Given a binary tree, fin ...

最新文章

  1. 用split分割文件和数据(笔记)
  2. libevent多线程使用bufferevent的那些事
  3. java上传ftp数据丢失_Java:将文件上传到FTP问题(数据包丢失) - java
  4. 安装oracle sqldeveloper
  5. 北斗导航 | GNSS技术在自动驾驶中的作用
  6. 【整数反转】算法优化笔记
  7. 使用线程池有以下几个目的
  8. java自建ocr完整示例_Java 7:完整的invokedynamic示例
  9. 【C/C++ 汇编语言 Verilog】越界截断——数据越界问题的多角度分析
  10. 这4个免费办公神器有多良心?用后就离不开,可惜一般人都不知道
  11. 信号问题可根除,苹果新款 iPhone 将搭载高通基带?
  12. 用C#创建Windows服务(Windows Services)并打包!
  13. Python 齿轮动力学建模及信号分析(纽马克算法)
  14. python切片详解_python切片及sys.argv[]用法详解
  15. 关于ESAPI无法打印debug级别日志
  16. macbook打开网页慢解决办法
  17. z-index诡异事件之背锅侠
  18. Linux飞鸽传书源码,Ubuntu 7.10下源码安装飞鸽传书IpMsg
  19. LabVIEW控制Arduino驱动数码管(基础篇—11)
  20. 芯片无忧的使用教程,ChipEasy芯片无忧如何检测U盘?U盘检测方法说明

热门文章

  1. 双指针找链表中点多种写法
  2. python多线程处理图片_Python斗图网多线程爬取图片
  3. 【带着canvas去流浪(10)】文字烟花
  4. Java数据结构与算法——插入排序
  5. 拆分一个字符串并把每个字符单独输出
  6. Spring 多视图配置
  7. Nignx平滑升级(1.8.0-1.8.1)
  8. HTTP文件浏览(静态文件+express4.x+md/code文件渲染)
  9. centOS防火墙中端口的开启和关闭
  10. WinDbg调试.NET程序入门