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.


题目标题:Tree

  这道题目给了我们一个二叉树,让我们找到它最小的深度,注意这里的最小深度是指root 到一个 leaf node。

  举个例子:  1

/

         2

      /

     3

  这种情况,最小depth 是3, 而不是因为1的right 是null ,最小depth 就等于1了。

  和求最大深度差不多,每次遇到null node 返回0。接着每一个点返回 left 和right 中小的那一个depth + 1。但是我们需要控制住一个情况,就是如果parent 的left 和right 其中有一个返回的是0, 意思是有一个null 点, 那么这里就不能取两个child 中小的那个了,因为一个点是null, 另一个不是,那么就说明,null点不是leaf 点。 这种情况只需要返回那个不是null 的点的 depth + 1 就可以了。

Java Solution:

Runtime beats 17.13%

完成日期:07/03/2017

关键词:Tree

关键点:设条件控制住left 和right 其中一个点是null 的这种情况,另外返回其他值

 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 public class Solution
11 {
12     public int minDepth(TreeNode root)
13     {
14         if(root == null)
15             return 0;
16
17         int left_depth = minDepth(root.left);
18         int right_depth = minDepth(root.right);
19
20         if(left_depth == 0 && right_depth != 0)
21             return right_depth + 1;
22         else if(left_depth != 0 && right_depth == 0)
23             return left_depth + 1;
24         else
25             return Math.min(left_depth, right_depth) + 1;
26     }
27 }

参考资料:N/A

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

转载于:https://www.cnblogs.com/jimmycheng/p/7114678.html

LeetCode 111. Minimum Depth of Binary Tree (二叉树最小的深度)相关推荐

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

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

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

  3. 111. Minimum Depth of Binary Tree 二叉树的最小深度

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

  4. LeetCode 111. Minimum Depth of Binary Tree--Java, Python解法--二叉树最小高度--迭代,递归

    题目地址:Minimum Depth of Binary Tree - LeetCode Given a binary tree, find its minimum depth. The minimu ...

  5. LeetCode: 111. Minimum Depth of Binary Tree

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

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

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

  7. 111. Minimum Depth of Binary Tree

    1.问题描述 Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along t ...

  8. LeetCode:104_Maximum Depth of Binary Tree | 二叉树的最大深度 | Easy

    要求:求二叉树的深度(二叉树的深度为最远叶子节点到根节点的距离,即根节点到最远叶子节点的距离) Given a binary tree, find its maximum depth.The maxi ...

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

最新文章

  1. 从上到下打印二叉树1
  2. 这段代码什么意思啊?
  3. ORACLE使用copy方式存储迁移,详细讲解Oracle数据库的数据迁移方法
  4. 二叉排序数的构造-理论
  5. 安卓系统曝漏洞!有人可能正在用你的手机秘密拍照
  6. Kerberos KDC not reachable
  7. gcc编译时rpath可以使用多个路径,用:分隔
  8. 气象报告是什么计算机领域,计算机辅助翻译系统在亚运气象服务方面的应用报告...
  9. 求解无约束最优化问题的共轭梯度法matlab程序,Matlab实现FR共轭梯度法
  10. Linux 安装字体
  11. en结尾的单词_en后缀形容词——动词
  12. 将Excel文件(xlsx)中的联系人和电话转换成vcard文件(.vcf),并用pyinstaller打包发布
  13. 论我是如何被自己搭建的聊天机器人气死的(自己的搞笑经历)
  14. 云端应用典型应用场景
  15. 分数换算小数补0法_小学数学概念+知识点顺口溜汇总+常用单位换算汇总
  16. PDF 预览和下载你是怎么实现的?
  17. python训练营 朋友圈点赞收费吗_千万不要随便在朋友圈点赞!
  18. 进销存系统_用户信息更新密码修改(3)
  19. Unity 3D项目 - 01 - 开始游戏主界面
  20. 致同助力国企改革不断迈出实质性步伐

热门文章

  1. 【python】快速对图像进行傅里叶变换
  2. seafile自建服务器,自建云盘系列——Seafile (支持分布式存储)
  3. NLP 算法工程师的学习、成长和实战经验
  4. 操作系统源代码_计算机自制操作系统(八):仿生DOS操作系统源代码
  5. Fixjs——事件回调的this
  6. html5游戏开发-零基础开发《圣诞老人送礼物》小游戏
  7. dnf如何快速拾取物品_极简断舍离|如何快速处理闲置物品?
  8. node sqlite 插入数据_Python基础系列讲解——如何使用自带的SQLite数据库
  9. linux的特殊符号与正则表达式
  10. 对用户密码进行加盐处理