[leetcode]_Maximum Depth of Binary Tree
第三道树的题目,我还是不会,我擦,怎么递归算法还是不能很好理解。看来还得好好研究下递归算法。
题目:求一棵树的最大深度。
思路:递归地求取左子树最大深度 和 右子树最大深度,返回较大值即为 整棵树的 最大深度。
代码:
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的更多相关文章
- 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 ...
- LeetCode——Maximum Depth of Binary Tree
LeetCode--Maximum Depth of Binary Tree Question Given a binary tree, find its maximum depth. The max ...
- [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 ...
- [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 ...
- 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 ...
- LeetCode - Minimum Depth of Binary Tree
题目: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the ...
- 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 ...
- Leetcode:Minimus Depth of Binary Tree
The problem description: Given a binary tree, find its minimum depth. The minimum depth is the numbe ...
- [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 ...
随机推荐
- php读取数据库数据,出现中文乱码(数据库中没有出现乱码)
添加header(“content-type: text/html; charset=utf-8”) php header() 函数向客户端发送原始的 HTTP 报头, 认识到一点很重要,即必须在任何 ...
- 类似 go get –u 的命令行参数实现
我们可能需要类似 go get –u -. 这样的方式来实现我们的应用,这时候我们无法简单地使用 flag.Parse 了,而是要用 FlagSet 了, 使用例子如下: package main ...
- Sq server 关于存储过程,触发器的一些理论简述
http://www.doc88.com/p-2905916227462.html
- nyoj 92 图像有用区域
点击打开链接 图像有用区域 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 "ACKing"同学以前做一个图像处理的项目时,遇到了一个问题,他需要摘取 ...
- How to make 9-patch image downloaded from the Network
Probably everyone, who is in touch with the Android world dealt with 9-patch term. It is an image in ...
- 使用 Cocos2d-x 3.1.1 创建 Windows Phone 8 游戏开发环境
cocos2d-x 是目前流行的游戏游戏开发框架,目前最新的版本是 3.1.1, 网上有些教程已经比较老了,本文将会介绍如何使用最新的 3.1.1 创建 Windows Phone 8 开发环境. 本 ...
- 基于jQuery的web在线流程图设计器GooFlow
简易的流程图设计控件,效果图: JavaScript源文件在GooFlow.js中,样式文件是GooFlow2.css.可以自定义样式. GooFlow_item类是每个项的样式属性. 但估计实现任务 ...
- Flex 中文字体终极解决方案
一直以来Flash对中文的支持就不是很好,很多人都发现很多汉字在Flex中无法设置粗体,就是其中一个表现,经过一晚上的折腾,终于突破了这个难题,其实,答案就在Adobe的官方教程里,只能怪自己英文水平 ...
- 模糊查询(LIKE)and (PATINDEX() . CHARINDEX())
SQL中的模糊查询一般来说使用模糊查询,大家都会想到LIKE select * from table where a like '%字符%' 如果一个SQL语句中用多个 like模糊查询,并且记录条 ...
- Centos安装arm-linux-gcc等交叉工具链
1.安装(仅以其中一个为例) 1.1 下载arm-linux-gcc (搜一下,很多的!) 1.2 解压:指定解压到根目录 tar xvzf arm-linux-gcc-4.4.3.tar.gz -C ...