【leetcode】104. Maximum Depth of Binary Tree
2016-05-02 18:12
441 查看
题目要求:
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.
即给定一个二叉树,求它的最大深度
思路:递归求左子树和右子树的深度,然后返回较大者
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int maxDepth(TreeNode root) {
if(root==null)
{
return 0;
}
int left = maxDepth(root.left);
int right = maxDepth(root.right);
return (left>right?left:right)+1;
}
}
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.
即给定一个二叉树,求它的最大深度
思路:递归求左子树和右子树的深度,然后返回较大者
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int maxDepth(TreeNode root) {
if(root==null)
{
return 0;
}
int left = maxDepth(root.left);
int right = maxDepth(root.right);
return (left>right?left:right)+1;
}
}
相关文章推荐
- uva 1424 (dp专组G题)
- C++基础实例(2)
- oracle sql文件转换成mysql sql
- 设计模式六大原则:单一职责
- 大量 python 爬虫源码分享--说说 python 爬虫这件小事
- Linux开机执行顺序
- spark内核架构解密(13)
- 内存分区和关于static的使用
- IntelliJ IDEA2016.1,gradle 创建 Java web 应用
- 常见的排序算法
- 利用volley去请求扇贝API获取个人信息
- 关于jdbc的数据库驱动类DriverManager.getConnection()参数
- GSON使用方法
- Linux内核分析笔记 与Linux内核开发理论
- Java中的杨慧三角编写
- hdu 1087 Super Jumping! Jumping! Jumping!
- FZU 2176 easy problem (DFS序+树状数组)
- java多线程中的死锁
- leetcode 344
- ActiveMQ实战(二)-代码示例