[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 ...
随机推荐
- [Flex] ButtonBar系列——labelFunction用户提供的函数,在每个项目上运行以确定其标签
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="ht ...
- Ext.MessageBox
Ext.require([ 'Ext.window.MessageBox', 'Ext.tip.*' ]); Ext.onReady(function(){ Ext.MessageBox.confir ...
- Hibernate 实体关联关系映射【转】
Hibernate关联关系映射目录│ ├─单向关联│ ├─ 一对一外键单向关联│ ├─ 一对一主键单向关联│ ├─ 一对一连接表单向关联│ ├─ 一对多外键单向关联│ ├─ 一对多 ...
- Codeforces 665D Simple Subset [简单数学]
题意: 给你n个数,让你从中选一个子集要求子集中的任何两个数相加都是质数. 思路: 一开始把自己坑了,各种想,后来发现一个简单的性质,那就是两个数相加的必要条件是这两个数之中必定一个奇数一个偶数,(除 ...
- POJ 2524
并查集思想,初始化每个元素的根节点为本身. 求解目标是求解存在几个集合.解决方案:查看有多少个根节点,表现在记忆数组上就是有多少个元素的根是它本身. #include<stdio.h> # ...
- [HDU 3535] AreYouBusy (动态规划 混合背包 值得做很多遍)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3535 题意:有n个任务集合,需要在T个时间单位内完成.每个任务集合有属性,属性为0的代表至少要完成1个 ...
- (转)C# MD5
本文原地址:http://blog.csdn.net/zhoufoxcn/article/details/1497099 作者:周公 代码如下: using System; using System. ...
- 紧张:飞测独家のJmeter秘籍,限量发放(续篇2)
飞测说:一些朋友问,我如何使用获取资料(点击这里获取)?小怪我花了点时间在这里介绍下该资料的功能和意义,另外也整理了一篇操作指引文档. 1.fiddler导出jmx格式的dll文件V4.0版本 功能: ...
- 应用层(一)HTTP服务访问基本流程和HTTP报文详解
HTTP属于TCP/IP模型中一个面向文本的应用层协议,所使用的服务器端口号的TCP中的80端口,通信双方在这个基础上进行通信. 每个服务器都有一个应用进程,时刻监听着80端口的用户访问请求.当有用户 ...
- notepad++ tab键用空格缩进
从工作那天开始到现在,写python代码一直用notepad++来写,尝试几次都改不回eclipse.o(╯□╰)o python脚本中,如果用制表符缩进,经常会报错,必须改用空格缩进代替. 之前设置 ...