第三道树的题目,我还是不会,我擦,怎么递归算法还是不能很好理解。看来还得好好研究下递归算法。

题目:求一棵树的最大深度。

思路:递归地求取左子树最大深度 和 右子树最大深度,返回较大值即为 整棵树的 最大深度。

代码:

public int maxDepth(TreeNode root) {
if(root == null) return 0; int leftHeight = 1,rightHeight = 1;
if(root.left != null) leftHeight += maxDepth(root.left);
if(root.right != null) rightHeight += maxDepth(root.right);
if(leftHeight > rightHeight) return leftHeight;
else return rightHeight;
}

昨天AC看的网络代码,今日回顾了一下,提交下面代码,顺利AC。

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

跟上面的原理一样,但这样写更清晰,感觉自己真的在进步啊。o(≧v≦)o~~好棒

[leetcode]_Maximum 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. 服务器压力测试 ab

    1.ab命令原理 Apache自带的ab命令模拟多线程并发请求,测试服务器负载压力,也可以测试nginx.lighthttp.IIS等其它Web服务器的压力(测试其它服务器时需单独下载ab压力测试工具 ...

  2. textarea文本换行和页面显示换行符

    在textarea里写的文本有换行,但是显示到页面后就不会自动换行,通过对数据分析发现textarea里的换行符是\n\r,然而HTML中的换行为<\br>解决办法有两种: 第一种:把文本 ...

  3. Hadoop有关的网站

    软件下载: http://archive.apache.org hbase对Hadoop的支持矩阵: https://hbase.apache.org/book.html#configuration

  4. XML小总结

    XHTML 标签都有固定含义,不能去创造新的标签. 而 XML 支持自定义标签,具有扩 展性. 定义 XML 文档结构有两种方法:DTD 和 XSD. XSD 本身就是 XML 文档结构,是继 DTD ...

  5. Cocos2d-x 3.0 事件系统【转】

    事件系统,是一个软件的核心组成部分.从小处讲它是应用程序内部各模块交互的设计模式,从大处讲,它是软件架构的组成模块.在现代软件开发中,操作系统通常通过一些预定义的事件,告知应用程序发生的一些事情如用户 ...

  6. JAVA的JDBC连接与sql操作

    一.前言 本文主要介绍怎样连接数据库.即JDBC的操作.以MySQL为例子. 前提是首先要将驱动jar包放入对应路径中. 二.过程说明 1.加载jdbc驱动程序 <span style=&quo ...

  7. Android GridView 指定行数,动态行宽,占满空间

    有时间我们需要 使用GridViw 让它占满父控件,例: 特别是在适配的时间比较麻烦,在不同的机型上可能分出下,下面空的太多,或有滚动条问题,; 下面说一下实现思路: 首先,设置GridView 为三 ...

  8. python-appium练习编写脚本时遇到问题

    遇到问题: 1.安卓4.2及以下系统无法识别resource-id属性 只能用text属性识别 2.输入中文无法识别 脚本最顶部增加#coding=utf-8 3.对象无法识别resource-id属 ...

  9. 慕课网-安卓工程师初养成-3-8 Java中的条件运算符

    来源:http://www.imooc.com/code/1306 条件运算符( ? : )也称为 “三元运算符”. 语法形式:布尔表达式 ? 表达式1 :表达式2 运算过程:如果布尔表达式的值为 t ...

  10. 循环链表Josephus问题(c,cpp)

    问题描述: 设有n个人围坐在一个圆桌周围,现从第s个人开始报数,数到第m个的人出列,然后从出列的下一个人重新开始报数,数到第m个的人又出列,.......,如此反复直到所有的人出列为止. Joseph ...