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

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

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

代码:

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. 九度OJ1061

    //C++ sort函数的多重排序 #include <iostream> #include<algorithm> #include<string> using n ...

  2. [ZOJ 1008]Gnome Tetravex (dfs搜索 + 小优化)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1008 题目大意:给你n*n的矩阵,每个格子里有4个三角形,分别是 ...

  3. linux创建用户和用户组

    Linux创建用户.用户组 及 删除 在创建用户时,需要为新建用户指定一用户组,如果不指定其用户所属的工作组,自动会生成一个与用户名同名的工作组.创建用户user1的时候指定其所属工作组users,例 ...

  4. How to Release the Temp LOB Space and Avoid Hitting ORA-1652 (文档 ID 802897.1)

    APPLIES TO: Oracle Database - Enterprise Edition - Version 8.1.5.0 and laterInformation in this docu ...

  5. 用js控制选项卡的隐藏与显示

    通过使用ul和div来,借助于jquery来实现选项卡的显示与隐藏 <form action="" method="post"> <div&g ...

  6. XAML学习笔记

         XAML是用于实例化.NET对象的标记语言,主要用于构造WPF界面.不同于WPF之前的Windows编程技术(WinForm,MFC及win32sdk),在WPF之中界面主要是在XAML中添 ...

  7. Grunt 之 watch 和 livereload

    现在 watch 中已经集成了 livereload ,所以把它们放在一起说明. watch 可以监控特定的文件,在添加文件.修改文件.或者删除文件的时候自动执行自定义的任务,比如 livereloa ...

  8. windows Phone 浏览器窗口的尺寸

    移动设备的屏幕一般都比PC小很多,移动设备的浏览器会将一个较大的  “虚拟”  窗口映射到移动设备的屏幕上,然后按一定的比例(3:1或2:1)进行缩放.也就是说当我们加载一个普通网页的时候,移动浏览器 ...

  9. Ubuntu上部署Ghost博客

    所有文章搬运自我的个人主页:sheilasun.me 刚刚成功把自己的ghost博客部署到Linode VPS上了,在这里回顾并顺便整理一下从购买域名到部署代码到服务器的整个过程. 购买域名 万网或者 ...

  10. JavaScript内置对象

    对象概述 JavaScript是一种基于对象的脚本语句,而不是面向对象的编程语言.对象就是客观世界存在的实体,具有属性和方法两方面特性. 访问对象的属性和方法的方式如下: 对象名.属性 对象名.方法名 ...