第五道树题,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. NIO与传统IO的区别

    传统的socket IO中,需要为每个连接创建一个线程,当并发的连接数量非常巨大时,线程所占用的栈内存和CPU线程切换的开销将非常巨大.使用NIO,不再需要为每个线程创建单独的线程,可以用一个含有限数 ...

  2. secureCRT如何远程桥接CentOS.

    1.将虚拟机的网络连接方式设置为桥接 2.关闭CentOS的防火墙,这里我是直接从页面上关闭的,没有使用命令 3.设置CentOS的ip为静态地址,不允许自动获取,这样远程连接不需要总修改地址.由于我 ...

  3. [HDU 4417] Super Mario (树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4417 题目大意:给你n个数,下标为0到n-1,m个查询,问查询区间[l,r]之间小于等于x的数有多少个 ...

  4. isIsomorphic

    超时版: /* Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if t ...

  5. java static 方法使用笔记

    有入参的static方法,可以正常使用 static的作用是申明:这是类的静态方法,什么时候都可以调用,可以传入入参,也可以不传. 上代码: 1.带静态方法的类: public class MakeP ...

  6. html 去掉input 获取焦点时的边框

    html中,当input标签获取焦点的时候(例如,当光标放在input框中准备输入值时), input标签外围会出现边框,有的时候我们需要去掉这个边框,可以使用css的outline:none;属性将 ...

  7. SQL Server参数化查询中应用Like

    一般情况下是SQL语句: Select * From Users Where UserName Like 'Lin%' Select * From Users Where UserName Like ...

  8. C++静态代码分析工具推荐——PVS-Studio

    长假归来,最近一直没更新,节前本来就想写这篇了,一直到今天才有时间. 关于静态代码分析在维基百科上可以查到很详细的介绍:https://en.wikipedia.org/wiki/List_of_to ...

  9. Flex4+BlazeDS+JAVA+MySql 构建J2EE工程 对用户信息进行管理实例

    要求 必备知识 本文要求基本了解 Adobe Flex编程知识和JAVA基础知识. 开发环境 MyEclipse10/Flash Builder4.6/Flash Player11及以上 演示地址 演 ...

  10. flex的Cairngorm框架

    由于要写flex的项目,接触了一段时间的Cairngorm框架,初步认识它是flex的一个mvc结构的框架实现了页面,调用相应方法的控制,和后台交互之间的三层之间的联系.Cairngorm框架主要包括 ...