Maximum Depth of Binary Tree Java
2015-07-08 11:40
387 查看
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.
/** * 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; } }
相关文章推荐
- 在 Java 中使用启发式搜索更快地解决问题
- Java IO最详解
- 深入探索 Java 热部署
- 使用 Cobertura 和反射机制提高 Java 单元测试中的代码覆盖率
- Java中到底有没有指针;同时注意引用和指针的区别
- 如何关闭Java线程
- springMVC的HandlerInterceptor拦截器
- Mybatis整合Spring
- Java为何大行其道
- 总结了Struts1与Struts2的12点区别
- Struts2的国际化实现
- springMVC上传文件
- Java操作百度身份证API
- java使用默认线程池踩过的坑(三)
- springMVC对异常处理的支持
- 基于注解的SpringMVC简单介绍
- java使用默认线程池踩过的坑(二)
- SpringMVC介绍之Validation
- java使用默认线程池踩过的坑(一)
- SpringMVC表单标签简介