leetcode:Maximum Depth of Binary Tree 【Java】
2016-03-07 15:47
507 查看
一、问题描述
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.
二、问题分析
利用递归思想。
三、算法代码
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 Math.max(left, right) + 1; } }
相关文章推荐
- 查看Eclipse版本号,及各个版本区别
- Spring MVC之@RequestBody, @ResponseBody 详解
- [springmvc]返回ModelAndView,视图解析不到jsp,而是去解析“'请求路径'+ ‘.jsp’”
- Bug:java.lang.IllegalStateException
- osgi与流行的框架(spring,struts,hibernate等)的集成
- Eclipse中ndk配置及示例
- spring 定时器配置
- Springmvc 执行流程
- leetcode:Balanced Binary Tree 【Java】
- java中Scanner类读取文件或控制台输入
- Struts+Spring+Hibernate处理Lob(Blob,Clob)
- MyEclipse快捷键大全
- 深入理解JVM : Java堆中对象创建、布局、访问全过程
- (java) 四则运算(包含分数)
- Java发邮件 实现步骤+代码
- eclipse配置hadoop插件
- myeclipse web工程发布出错
- JAVA邮件发送的简单实现
- Struts2的类型转换及输入校验(集合类型的使用)
- java中的类的初始化顺序