Java [Leetcode 104]Maximum Depth of Binary Tree
题目描述:
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;
else
return Math.max(maxDepth(root.left) + 1, maxDepth(root.right) + 1);
}
}
Java [Leetcode 104]Maximum Depth of Binary Tree的更多相关文章
- LeetCode 104. Maximum Depth of Binary Tree C++ 解题报告
104. Maximum Depth of Binary Tree -- Easy 方法 使用递归 /** * Definition for a binary tree node. * struct ...
- leetcode 104 Maximum Depth of Binary Tree二叉树求深度
Maximum Depth of Binary Tree Total Accepted: 63668 Total Submissions: 141121 My Submissions Question ...
- [LeetCode] 104. Maximum Depth of Binary Tree 二叉树的最大深度
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- (二叉树 BFS DFS) leetcode 104. 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 104. Maximum Depth of Binary Tree二叉树的最大深度 C++/Java
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- Java for LeetCode 104 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 104 Maximum Depth of Binary Tree ----- java
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- LeetCode 104. 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 104. Maximum Depth of Binary Tree
Problem: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along ...
随机推荐
- PID控制器的数字实现及C语法讲解
PID控制器的数字实现及C语法讲解 概述 为方便学习与交流,根据自己的理解与经验写了这份教程,有错误之处请各位读者予以指出,具体包含以下三部分内容: (1) PID数字化的推导过程(实质:微积分的近 ...
- Nginx upstream的5种权重分配方式
.轮询(默认) 每个请求按时间顺序逐一分配到不同的后端服务器,后端服务器down掉,能自动剔除 .weight 指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况. upstre ...
- 使用Yeoman搭建 AngularJS 应用 (7) —— 让我们搭建一个网页应用
原文地址:http://yeoman.io/codelab/preview-inbrowser.html 开启你的服务 运行Grunt任务,通过输入下面的命令来创建一个本地Node的http服务,地址 ...
- 1048: [HAOI2007]分割矩阵 - BZOJ
Description 将一个a*b的数字矩阵进行如下分割:将原矩阵沿某一条直线分割成两个矩阵,再将生成的两个矩阵继续如此分割(当然也可以只分割其中的一个),这样分割了(n-1)次后,原矩阵被分割成了 ...
- @Entity设置实体lazy = false
问题描述 在通过Hibernate查询Bean信息时报以下异常信息: org.hibernate.LazyInitializationException: could not initialize p ...
- UIScrollView的坑--UINavigationController Push后位置变化
今天在使用UIScrollView的时候遇到了一个问题,记录一下.如果这个记录有幸被您搜索到,或许对您有些帮助. 今天有这样一个需求: 在一个由导航条控制的页面中.需要显示一些信息,目前已经有10多行 ...
- 酷摄影:关于梦 - Miki takahashi
这组摄影来自于日本东京摄影师 Miki takahashi 是一组双重曝光摄影,分开看也许很平常,但是结合在一起却非常有韵味. [gallery]
- Spring MVC Checkbox And Checkboxes Example
In Spring MVC, <form:checkbox /> is used to render a HTML checkbox field, the checkbox values ...
- linux 使用kill命令杀死进程的几个办法
常规篇: 首先,用ps查看进程,方法如下: $ ps -ef ……smx 1822 1 0 11:38 ? 00:00:49 gnome-terminalsmx ...
- cocos2d-x 锚点,位置==》动手实验记录 多动手... :)
总结: 1:cocos2d-x的位置由锚点和Position位置 共同决定的.2: cocos2d-x,当位置不设置或者为零的时候, 子节点的锚点位置永远位于父节点的左小角的地方3:我们的自己的游戏编 ...