给定一个二叉树,找出其最小深度。
最小深度是从根节点到最近叶节点的最短路径的节点数量。
详见:https://leetcode.com/problems/minimum-depth-of-binary-tree/description/

Java实现:

递归实现:

  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. * int val;
  5. * TreeNode left;
  6. * TreeNode right;
  7. * TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. class Solution {
  11. public int minDepth(TreeNode root) {
  12. if(root==null){
  13. return 0;
  14. }
  15. int minLeft=minDepth(root.left);
  16. int minRight=minDepth(root.right);
  17. if(minLeft==0||minRight==0){
  18. return minLeft>=minRight?minLeft+1:minRight+1;
  19. }
  20. return Math.min(minLeft,minRight)+1;
  21. }
  22. }

非递归实现:

  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. * int val;
  5. * TreeNode left;
  6. * TreeNode right;
  7. * TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. class Solution {
  11. public int minDepth(TreeNode root) {
  12. if(root==null){
  13. return 0;
  14. }
  15. LinkedList<TreeNode> que=new LinkedList<TreeNode>();
  16. que.offer(root);
  17. int node=1;
  18. int level=1;
  19. while(!que.isEmpty()){
  20. root=que.poll();
  21. --node;
  22. if(root.left==null&&root.right==null){
  23. return level;
  24. }
  25. if(root.left!=null){
  26. que.offer(root.left);
  27. }
  28. if(root.right!=null){
  29. que.offer(root.right);
  30. }
  31. if(node==0){
  32. node=que.size();
  33. ++level;
  34. }
  35. }
  36. return level;
  37. }
  38. }

111 Minimum Depth of Binary Tree 二叉树的最小深度的更多相关文章

  1. [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)

    [Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...

  2. [LeetCode] 111. Minimum Depth of Binary Tree 二叉树的最小深度

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

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

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

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

  5. [Leetcode] The minimum depth of binary tree二叉树的最小深度

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

  6. Leetcode 111 Minimum Depth of Binary Tree 二叉树

    找出最短的从叶子到根的路径长 可以回忆Maximum Depth of Binary Tree的写法,只不过在!root,我把它改成了10000000,还有max函数改成了min函数,最后的值如果是1 ...

  7. LeetCode:111_Minimum Depth of Binary Tree | 二叉树的最小深度 | Easy

    要求:此题正好和Maximum Depth of Binary Tree一题是相反的,即寻找二叉树的最小的深度值:从根节点到最近的叶子节点的距离. 结题思路:和找最大距离不同之处在于:找最小距离要注意 ...

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

  9. (二叉树 BFS DFS) leetcode 111. Minimum Depth of Binary Tree

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

随机推荐

  1. 使用XMLHttpRequest

    请求种类 通过XMLHttpRequest的请求可以通过同步和异步的方式获取数据,请求的种类在XMLHttpRequest的open()方法的第三三个可选参数async设置.如果这个参数是true或者 ...

  2. Android四种启动模式

    四种启动模式 standard(默认) singleTop singleTast singleInstance standard(默认) 系统默认的启动模式. Android是使用返回栈来管理活动的, ...

  3. 23.java方法的深入

    深入: public class MethodTest05{ public static void main(String[] args){ int i=m1(ture); System.out.pr ...

  4. TModJS:使用tmodjs

    ylbtech-TModJS:使用tmodjs 1.返回顶部 1. 1.安装 npm install -g tmodjs 2.配置 我的模板都放在tpl文件夹中,htmls用于存放模板页面,每一个后缀 ...

  5. sqlserver 截取字符串

    **/*******/*****/1399/* 我要取第3个'/'与第4个'/'中的内容,就是1399 create table ta( col varchar(100)) insert ta sel ...

  6. sqlserver:rank() over()函数

    先前在oracle数据库中接触过over()函数的一系列使用,但是在实际的sql开发中并没有太多的使用,这次在sqlserver的sql拼写中重新遇到这个函数,故结合网上的例子和项目中实际中使用,将其 ...

  7. http://blog.sina.com.cn/s/blog_6145ed810102vr8k.html

    http://blog.sina.com.cn/s/blog_6145ed810102vr8k.html

  8. CNN相关资料

    转子http://blog.csdn.net/qianqing13579/article/details/71076261 前言 入职之后,逐渐转到深度学习方向.很早就打算写深度学习相关博客了,但是由 ...

  9. 1.5 Hive初步使用和安装MySQL

    一.HQL初步试用 1.创建一个student表 #创建一个student表 hive> create table student(id int, name string) ROW FORMAT ...

  10. 坑暗花明:又遇 .NET Core 中 System.Data.SqlClient 查询缓慢的问题

    之前发布过一篇博文 下单快发货慢:一个 JOIN SQL 引起 SqlClient 读取数据慢的奇特问题,当时遇到的问题是从 SQL Server 2008 R2 中查询获取 100 条记录竟然耗时 ...