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

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

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

代码:

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. jQuery的jsonp跨域是这么回事.

    实现跨域请求的有iframe,img,script中的src属性.那么jquery是如何解决跨域请求的呢? 一:项目jsonp2中有个app.js文件,代码如下: function app(json) ...

  2. [SQL]reName存储过程

    exec sp_helptext aa--应用sp_helptext查看存储过程的定义文本 exec sp_depends aa --通过sp_depends查看存储过程的相关性 exec sp_he ...

  3. java 的数据类型

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

  4. 在weblogic11g上发布项目遇到的一个错误(不支持web-app_3_0)

    错误信息如下: 消息图标 - 错误 [HTTP:101064][WebAppModule(ssh1:ssh1.war)] Error parsing descriptor in Web appplic ...

  5. OC基础(26)

    集合对象的内存管理 Copy copy与内存管理 @property中的copy关键字 自定义的类实现copy操作 *:first-child { margin-top: 0 !important; ...

  6. How I came to find Linux

    http://ianmurdock.com/post/how-i-came-to-find-linux/ lan murdock August 17, 2015 I saw my first Sun ...

  7. android 开发进阶自定义控件 类似 TextView

    开发自定义控件的步骤: 1. 继承View: 2.重写构造函数并构造方法中获得我们自定义的属性. 3. 重写onDraw, 4.重写onMeasure 等函数 一.自定义View的属性,首先在res/ ...

  8. iptable怎么用?

    iptables -A FORWARD -s 10.0.0.0/8  -p tcp  --dport  80  -j DROP [拒绝转发来自10.0.0.0/8网段,目的端口是80的数据包]

  9. 【caffe-windows】 caffe-master 之 cifar10 超详细

    本教程尽量详细,大多步骤都有图,如果运行出错,请先对照自己的文件是否和图上的一样,包括标点啊,空格啊,斜杠,反斜杠啊之类的小细节. 本例程是在 win10 64位   caffe-master     ...

  10. 这些优化 Drupal 网站速度的超简单办法,你忽略了多少?

    “怎么样能让我的 Drupal 网站更快一些?”是我们最常遇到的一个问题.站点速度确实非常重要,因为它会影响你的 SEO排名效果.访客是否停留以及你自己管理网站所需要的时间. 今天我们就来看看那些通过 ...