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 的这种情况,另外返回其他值

 /**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution
{
public int minDepth(TreeNode root)
{
if(root == null)
return 0; int left_depth = minDepth(root.left);
int right_depth = minDepth(root.right); if(left_depth == 0 && right_depth != 0)
return right_depth + 1;
else if(left_depth != 0 && right_depth == 0)
return left_depth + 1;
else
return Math.min(left_depth, right_depth) + 1;
}
}

参考资料:N/A

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

LeetCode 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 111 Minimum Depth of Binary Tree 二叉树

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

  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. (二叉树 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 ...

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

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

  8. leetcode 111 minimum depth of binary tree

    problem description: Given a binary tree, find its minimum depth. The minimum depth is the number of ...

  9. leetcode 111 Minimum Depth of Binary Tree ----- java

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

随机推荐

  1. paxos 算法原理学习

    下面这篇关于paxos分布式一致性的原理,对入门来说比较生动有趣,可以加深下影响.特此博客中记录下. 讲述诸葛亮的反穿越 0.引子 一日,诸葛亮找到刘备,突然献上一曲<独角戏>,而后放声大 ...

  2. Struts2第七篇【介绍拦截器、自定义拦截器、执行流程、应用】

    什么是拦截器 拦截器Interceptor-..拦截器是Struts的概念,它与过滤器是类似的-可以近似于看作是过滤器 为什么我们要使用拦截器 前面在介绍Struts的时候已经讲解过了,Struts为 ...

  3. 【轉】使用jQuery播放/暂停 HTML5视频

    jQuery不可以使用play()方法,但js是可以的: document.getElementById('movie1').play();   解决方法:play并不是jQuery的函数,而是DOM ...

  4. JS中this到底指哪个对象

    忘了原作者是谁了   自己也添加了一些东西  勉强可以观看一下 元素就不写了   可以自己添一下试试 先看这段代码 target.onclick = function(){ console.log(t ...

  5. react基础(2)

    react基础(1):介绍了如何创建项目,总结了JSX.组件.样式.事件.state.props.refs.map循环,另外还讲了mock数据和ajax 还是用 react基础1 里创建的项目继续写案 ...

  6. 15 Validation

    一.模型选择问题 如何选择? 视觉上 NO 不是所有资料都能可视化;人脑模型复杂度也得算上 通过Ein NO 容易过拟合;泛化能力差 通过Etest NO 能保证好的泛化,不过往往没法提前获得测试资料 ...

  7. NOIP2017SummerTraining0717

    个人感受:自己水平是真的差劲,和他们不是一个档次的,第二题,如果不是陈载元暴力过了,我也不会那么早去A了第二题,第一题真的是无语,以前做到过,还想到了每个对应值a[i]-i,但是没想出来,真的是 可惜 ...

  8. 混合高斯模型(Mixtures of Gaussians)和EM算法

    这篇讨论使用期望最大化算法(Expectation-Maximization)来进行密度估计(density estimation). 与k-means一样,给定的训练样本是,我们将隐含类别标签用表示 ...

  9. python邮件SMTP的GUI编程

    写的是python中smtp的gui编程,用的163邮箱给qq邮箱发送邮件做测试,如果你发现你的发送失败,试着用以下方法解决: 1.网页登陆你的邮箱,设置中查看smtp是否开启,比如163邮箱的smt ...

  10. Angular学习笔记(一)

    本文为原创文章,转载请标明出处 目录 架构 模块 组件 模板 元数据 数据绑定 指令 服务 依赖注入 模板与数据绑定 1. 架构 模块 Angular 应用是模块化的,并且 Angular 有自己的模 ...