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.

解题思路:注意minimum depth最后遍历的那个点,left right都必须为null,JAVA实现如下:

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

Java for LeetCode 111 Minimum Depth of Binary Tree的更多相关文章

  1. Java实现LeetCode 111. Minimum Depth of Binary Tree

    /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * Tre ...

  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 ☆(二叉树的最小深度)

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

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

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

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

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

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

  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. (二叉树 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. innodb事务锁

    计算机程序锁   控制对共享资源进行并发访问 保护数据的完整性和一致性   lock  主要是事务,数据库逻辑内容,事务过程 latch/mutex 内存底层锁:   更新丢失 原因: B的更改还没有 ...

  2. Iosapp升级版本步骤

    1.打开地址https://itunesconnect.apple.com/WebObjects/iTunesConnect.woa,登录进入 2.选择ManageYour App,选中你要升级的那个 ...

  3. Android Spinner In Toolbar

    As the title of the post suggest in this tutorial we will see how to have spinner widget inside the ...

  4. IDEA使用Maven打包时如何去掉测试阶段

    如图

  5. MyEclipse 中自定义日期格式

    从数据库中读出Data数据: 而想实现的是这样: 解决办法: 1. 在这个类里添加自定义的变量birthf: public abstract class AbstractUsers implement ...

  6. 一入python深似海--range()、list与for

    range使用方法 使用python的人都知道range()函数非常方便,今天再用到他的时候发现了非常多曾经看到过可是忘记的细节. 这里记录一下: range(1,5)#代表从1到5(不包括5) [1 ...

  7. fastjson设置生成的json属性的顺序

    目标JSON顺序: {,,} 1.url2.w3.h4.s 如果使用默认情况生成,则顺序为: w,h,s,url 在fastJson中,可以使用@JSONField来指定属性顺序(需要1.1.42以上 ...

  8. 2016.11.25 activiti的配置文件解析

    参考来自activiti的用户手册.   activiti的配置文件解析 1.processEngine的配置 注意,单独创建流程引擎与spring方式创建流程引擎是不一样的,区别在于:process ...

  9. HTTP错误状态码定位与解决

    实践总结 本次基于对500错误定位为例,给大家讲解整个分析过程与解决方法. 1.本次实践为HTTP错误状态码定位提供一个高效.精确的定位方式,不仅仅局限于500错误. 2.针对500错误本身,可以基于 ...

  10. Ubuntu系统经常使用操作指令说明

    使用U盘拷贝压缩文件 文件的压缩方法详见:3.6文件归档压缩及其释放 U盘直接插入机器USB接口.等待自己主动弹出窗体,在弹出窗体选择"文件->打开终端",打开的终端当前文件 ...