[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 ...
随机推荐
- 如何在XAMPP中设置多个网站
xampp 是一个非常方便的本地 apache + php + mysql 的调试环境,在本地安装测试 WordPress 等各种博客.论坛程序非常方便.今天我们来给大家介绍一下,如何使用 XAMPP ...
- 九度OJ1207
题目给你了一个很大的n,然后让你去计算它的质因数.对N进行开方得到的是一个大约在32000左右的数,我们可以用埃氏筛法进行素数打表.对所有prime[i]<=sqrt(n),分别看prime[i ...
- turing 项目引用
1.友盟自动更新 2.友盟统计 3.友盟消息推送 http://www.bejson.com/json2javapojo/ 引用bejson 解析JSON生成类,数组 private List< ...
- 第一课 opengl简介
1. 什么是opengl: opengl是图形硬件的一种软件接口. 2. opengl对场景中的图像进行渲染时所执行的主要图形操作 1)根据几何图元创建形状,从而建立物体的数学描述. 2)在三维空间中 ...
- ubuntu设置vim语法高亮显示和自动缩进
转自:http://nichael1983.blog.163.com/blog/static/114969433201002711850604/ 今天自己学习使用vim,当我在vim中输入程序时,默认 ...
- python学习笔记(递归函数)
博主看了看递归.说的简单点就是程序里面再调用程序本身,或者是方法里面再调研方法本身.或者是函数里面再调研函数本身 用于什么场景呢,博主这里是父子节点排序,父子节点的查询 直接上代码: #!/usr/b ...
- 使用ImageNet在faster-rcnn上训练自己的分类网络
具体代码见https://github.com/zhiyishou/py-faster-rcnn 这是我对cup, glasses训练的识别 faster-rcnn在fast-rcnn的基础上加了rp ...
- 普通session vs MemcachedSession vs RedisSession
一.普通session(数据存储在内存中) #!/usr/bin/env python # -*- coding:utf-8 -*- from hashlib import sha1 import o ...
- 多XML追加操作
假设要统计当前系统中所有的试卷进行分析,试卷是以XML格式存储的,所有这就需要将所有零散的XML文件整合起来,处理成一个完整的XML文件,进行分析, 下面是简单额处理方法: 当前XML文件格式: &l ...
- grep使用
grep常用的使用方法 grep –rns “match_content”filename 查看匹配内容的行 find /path –name “*.h” –o –name “*.cpp” | xar ...