第五道树题,10分钟之内一遍AC。做树题越来越有feel~

题目:求一棵树从root结点到叶子结点的最短路径。

思路:仍然是递归。如果一个结点的左右子树任意一边为Null,该子树的minDepth应为非null子树的高度+1;如果一个结点的左右子树两边都非Null,则该子树的minDepth应为两个子树的较小值

代码:

public int minDepth(TreeNode root) {
if(root == null) return 0; if(root.left == null && root.right == null) return 1;
else if(root.left == null || root.right == null)
//如果该结点的left或者right为null,则该结点的depth应该为非null边子树高度 + 1
return minDepth(root.left) + minDepth(root.right) + 1;
else
//如果该结点的left和right都不等于null ,则该结点的depth应该为两边子树高度的较小值 + 1
return Math.min(minDepth(root.left) , minDepth(root.right)) + 1;
}

if语句的前两种情况本可以合并书写,考虑到合并起来理解性太差了,就这样了。第二个return 中,由于 肯定有一个minDepth的返回值是0,所以我就直接取它们的和值了,不分两种情况来写。

[leetcode]_Minimum Depth of Binary Tree的更多相关文章

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

  2. LeetCode——Maximum Depth of Binary Tree

    LeetCode--Maximum Depth of Binary Tree Question Given a binary tree, find its maximum depth. The max ...

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

  4. [LeetCode] Maximum Depth of Binary Tree 二叉树的最大深度

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

  5. LeetCode: Minimum Depth of Binary Tree 解题报告

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

  6. LeetCode - Minimum Depth of Binary Tree

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

  7. Leetcode Maximum Depth of Binary Tree

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

  8. Leetcode:Minimus Depth of Binary Tree

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

  9. [Leetcode] Maximum depth of binary tree二叉树的最大深度

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

随机推荐

  1. Linux文件夹文件创建、删除

    Linux删除文件夹命令 linux删除目录很简单,很多人还是习惯用rmdir,不过一旦目录非空,就陷入深深的苦恼之中,现在使用rm -rf命令即可.直接rm就可以了,不过要加两个参数-rf 即:rm ...

  2. Adobe AIR socket complicating 导致 socket RST

    基于socket实现HTTP协议 并发请求AB,A为HTTP请求,B为socket请求. A的请求服务器返回数据很短,包体长度只有35,且客户端收到包头后就断开连接,同时A连接的服务器也断开了连接, ...

  3. java 的数据类型

    java 的数据类型有基本类型和引用类型 java的类的关系:有继承,有依赖,有关联,聚合,组成.

  4. T4 assembly

    In a T4 template the executing assembly is not yours but one from the T4 engine. To access types fro ...

  5. Hive基础之COALESCE用法

    语法: COALESCE(T v1, T v2, …) 返回参数中的第一个非空值:如果所有值都为NULL,那么返回NULL 以emp表为例: desc emp; empno int None enam ...

  6. android 提高进程优先级 拍照永不崩溃(闪退)

    首先科普一下Android系统进程的优先级: 当系统的内存不足时, android系统将根据进程优先级选择杀死一些不太重要的进程. 进程优先级从高到低分别为: 1. 前台进程. 以下的进程为前台进程: ...

  7. CODESOFT 2015中的二维码该怎样生成

    由于二维条码具有储存量大.保密性高.追踪性高.抗损性强.备援性大.成本便宜等特性,其应用 渐趋广泛,因此二维码的制作对于CODESOFT条码设计软件的用户来讲可谓司空见惯.我们最常见的二维码要数QR码 ...

  8. LibreOffice源码编译以及生成VS项目

    最权威的社区链接:https://wiki.documentfoundation.org/Development/BuildingOnWindows 也许英文好的人直接看wiki上的说明就能很容易的编 ...

  9. 好文EF

    http://www.cnblogs.com/zhaopei/p/5721789.html#autoid-0-0 http://www.cnblogs.com/zhaopei/p/5746414.ht ...

  10. 【LeetCode】19. Remove Nth Node From End of List

    题目: 思路:如果链表为空或者n小于1,直接返回即可,否则,让链表从头走到尾,每移动一步,让n减1. 1.链表1->2->3,n=4,不存在倒数第四个节点,返回整个链表 扫过的节点依次:1 ...