leetcode-Maximum Depth of Binary Tree
2015-11-03 19:06
302 查看
Given a binary tree, find its maximum depth.
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
求树的最大深度,也就是求左子树或右子树的最大深度,通过递归,得到最大深度。
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
求树的最大深度,也就是求左子树或右子树的最大深度,通过递归,得到最大深度。
public class Solution { public int maxDepth(TreeNode root) { if(root==null) return 0; if(root.left==null&&root.right==null) return 1; else { return (1+Math.max(maxDepth(root.left),maxDepth(root.right))); } } }
相关文章推荐
- LeetCode Bulls and Cows (简单题)
- Mac OS X pl2303 的驱动下载
- java处理高并发高负载类网站的优化方法
- 设计模式之原型模式(深入理解OC中的NSCopying协议以及浅拷贝、深拷贝)
- OC基础第三讲):继承 初始化方法 便利构造器
- 如何一个命令停止某个端口的服务进程
- POJ2418 Hardwood Species(二叉搜索树+字典树+MAP)
- 十分钟搞清字符集和字符编码
- pluto实现分析(2)——代码函数调用关系
- Java Iterator与ListIterator的主要区别
- JavaScript学习点滴 call、apply的区别
- 关于Android中的枚举【4种】
- 无题
- java连接mysql删除操作
- 面试感悟——dmall
- 51单片机控制直流电机
- php上传七牛实例
- Java常用的设计模式13:常用设计模式之桥接模式(结构型模式)
- 第八周项目3 顺序串算法(3)
- Web服务器