题目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. 何時需要重启 OFBiz

    你在做如下更改時需要重新启動OFBiz服務器: - Java文件(記得要重新編譯) - 配置/.properties文件 - entitymodel或entitygroup XML定義文件 - 服務或 ...

  2. [水煮 ASP.NET Web API2 方法论](1-2)在 WebForm 应用程序中添加 ASP.NET Web API

    问题 怎么样将 Asp.Net Web Api 加入到 Asp.Net Web From 应用程序中 解决方案 在 Visual Studio 2013 中,创建新的 Web From,可以直接在&q ...

  3. js正则表达大合集【转载自:http://caibaojian.com】

    [注明原文链接吧]:http://caibaojian.com 1 用户名正则 //用户名正则,4到16位(字母,数字,下划线,减号) var uPattern = /^[a-zA-Z0-9_-]{4 ...

  4. nginx静态资源浏览器缓存

    1.缓存介绍 作用:提升用户体验,减少服务器压力 浏览器无缓存:浏览器发起请求->无缓存->请求WEB服务器->web服务器检查是否有更新(没有更新返回304)->呈现 浏览器 ...

  5. CentOS 7 Docker

    安装docker yum install docker 查看docker docker version 这是因为docker还没有运行,需要启动docker 启动docker systemctl st ...

  6. PTA L1-020 帅到没朋友 团体程序设计天梯赛-练习集

    L1-020 帅到没朋友(20 分)   当芸芸众生忙着在朋友圈中发照片的时候,总有一些人因为太帅而没有朋友.本题就要求你找出那些帅到没有朋友的人. 输入格式: 输入第一行给出一个正整数N(≤),是已 ...

  7. Java工具类-加密算法

    import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.securit ...

  8. PHP超全局变量数组

    以下8个变量都是数组变量,又称为“预定义变量” 1.$_GET : 把数据通过地址栏传递到服务器,这是方式必须是$_GET方式传递: 2.$_POST : 通过表单发送的数据必须是POST方式: 3. ...

  9. Eclipse line number

  10. Windows下安装Redis服务及安装PHP的Redis扩展

    Redis是一个开源的使用ANSI C语言编写.遵守BSD协议.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API. 它通常被称为数据结构服务器,因为值(valu ...