题目URL:https://leetcode.com/problems/minimum-depth-of-binary-tree/

111. Minimum Depth of Binary Tree

My Submissions

Question
Total Accepted: 94580 Total
Submissions: 312802 Difficulty: Easy

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.

Subscribe to see which companies asked this question

Show Tags
Show Similar Problems
Have you met this question in a real interview? 

Yes

 

No

Discuss

    求一棵二叉树。离根近期的叶子的高度。递归找出两棵子树的最小高度加一便可求解。

    我的AC代码

public class MinimumDepthofBinaryTree {

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

LeetCode OJ Minimum Depth of Binary Tree 递归求解的更多相关文章

  1. LeetCode OJ——Minimum Depth of Binary Tree

    http://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ 贡献了一次runtime error,因为如果输入为{}即空的时候,出现了c ...

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

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

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

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

  4. [Leetcode][JAVA] Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree

    Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...

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

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

  7. Java [Leetcode 111]Minimum Depth of Binary Tree

    题目描述: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along th ...

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

  9. LeetCode(37)-Minimum Depth of Binary Tree

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

随机推荐

  1. Trapping Rain Water——经典的双边扫描问题

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  2. Zabbix历史数据库迁移 及分区

    https://blog.csdn.net/hkyw000/article/details/78971201?utm_source=blogxgwz6

  3. nutch 抓取需要登录的网页

    题记:一步一坑,且行且珍惜 最近接到任务,要利用nutch去抓取公司内部系统的文章,可是需要登录才能抓到.对于一个做.net,不熟悉java,不知道hadoop,很少接触linux的我,这个过程真是艰 ...

  4. web前端零基础入门学习!前端真不难!

    现在互联网发展迅速,前端也成了很重要的岗位之一,许多人都往前端靠拢,可又无能为力,不知所措,首先我们说为什么在编程里,大家都倾向于往前端靠呢?原因很简单,那就是,在程序员的世界里,前端开发是最最简单的 ...

  5. 通过javascript进行UTF-8编码

    通过javascript进行UTF-8编码 javascript的字符集: javascript程序是使用Unicode字符集编写的.Unicode是ASCII和Latin-1的超集,并支持地球上几乎 ...

  6. 微软企业库5.0 学习之路——第四步、使用缓存提高网站的性能(EntLib Caching)

    首先先补习下企业库的Caching Application Block的相关知识: 1.四大缓存方式,在Caching Application Block中,主要提供以下四种保存缓存数据的途径,分别是 ...

  7. android studio偏好设置

    1.主题设置,可以选择白色主题及黑色主题 2.代码字体大小 3.生成新的主题 主题命名 4.加入代码时,自动引用库 5.合作菜单生成菜码 6.命名空间设置 字段设置为大写,静态字段设置为小写 SDK设 ...

  8. 用Python创建XML(转)

    在官方网站没有找到多少有用的知识.结果在Python and XML: An Introduction找到了一篇教程,抽空对照做,然后再总结分享出来.先来一个简单的: from xml.dom.min ...

  9. 原本就有mysql,安装phpstudy使用里面自带的mysql导致原来的没服务

    电脑中之前安装了mysql,正常服务中,但做项目的需要,安装了phpStudy,它里面自带了mysql,启动phpstudy里面的mysql后在用原来的就没服务了, 到电脑管理服务中也没有发现mysq ...

  10. 洛谷P2520向量

    题目传送门 看到数据范围其实就可以确定这是一道结论题. 首先分析,给定你的向量的两个坐标a,b有八种组合方式可以用,但实际上整理一下可以得出实际上只有五种,x/y ±2a,x/y ±2b,x+a,y+ ...