【LeetCode-面试算法经典-Java实现】【104-Maximum Depth of Binary Tree(二叉树的最大深度)】
【104-Maximum Depth of Binary Tree(二叉树的最大深度)】
【LeetCode-面试算法经典-Java实现】【全部题目文件夹索引】
原题
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.
题目大意
给定一棵两叉树。求它的最大深度。
解题思路
递归求解。递归公式
f(n) = 0; n=null,
f(n) = 1+ max(f(n左), f(n右))
代码实现
树结点类
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 if (root.left == null && root.right == null) {
return 1;
} else {
int left = maxDepth(root.left);
int right = maxDepth(root.right);
return 1 + (left > right ? left : right);
}
}
}
评測结果
点击图片,鼠标不释放。拖动一段位置,释放后在新的窗体中查看完整图片。
特别说明
欢迎转载,转载请注明出处【http://blog.csdn.net/derrantcm/article/details/47354355】
【LeetCode-面试算法经典-Java实现】【104-Maximum Depth of Binary Tree(二叉树的最大深度)】的更多相关文章
- [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 ...
- [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 l ...
- 104 Maximum Depth of Binary Tree 二叉树的最大深度
给定一个二叉树,找出其最大深度.二叉树的深度为根节点到最远叶节点的最长路径上的节点数.案例:给出二叉树 [3,9,20,null,null,15,7], 3 / \ 9 20 / ...
- LeetCode Javascript实现 258. Add Digits 104. Maximum Depth of Binary Tree 226. Invert Binary Tree
258. Add Digits Digit root 数根问题 /** * @param {number} num * @return {number} */ var addDigits = func ...
- leetcode 104 Maximum Depth of Binary Tree二叉树求深度
Maximum Depth of Binary Tree Total Accepted: 63668 Total Submissions: 141121 My Submissions Question ...
- [LintCode] 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] 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]题解(python):104 Maximum Depth of Binary Tree
题目来源 https://leetcode.com/problems/maximum-depth-of-binary-tree/ Given a binary tree, find its maxim ...
- 【LeetCode】Maximum Depth of Binary Tree(二叉树的最大深度)
这道题是LeetCode里的第104道题. 给出题目: 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例: 给定 ...
随机推荐
- 【Pycharm】【HTML】注释问题
学习HTML中,遇到的注释前存在空行的问题: 只要找到Pycharm设置中:勾选去掉即可
- Intellij IDEA中修改项目名称
如下图红色标识所示: 修改方法见下图:
- 洛谷 P2108 学英语
P2108 学英语 题目描述 为了适应紧张的大学学习生活,小Z发愤图强开始复习巩固英语. 由于小Z对数学比较有好感,他首先复习了数词.小Z花了一整天的时间,终于把关于基数词的知识都搞懂了.于是小Z非常 ...
- RK3066 实现LED闪烁的代码分析
实现LED灯的闪烁,须要在驱动里加入一个定时器函数,详细实现涉及到了LED GPIO驱动.用户空间程序调用驱动程序. 1.首先来看LED设备驱动注冊过程,代码位于../kernel/drivers/l ...
- layoutParams-动态更改某个控件的margin
其实它的方法也非常的简单,如下 LinearLayout.LayoutParams layoutParams = (LayoutParams) bt1.getLayoutParams(); int a ...
- IE中实现placeholder
简介:IE本身不支持Placeholder这种先进的特性,但是我们又必须且仅仅支持IE,所以网上找了一个支持placeholder的方法 考虑版权,以及知识产权原因,只放链接: http://blog ...
- 很好的资源 for android
//texttospeach http://examples.javacodegeeks.com/android/core/text-to-speech/android-text-to-speech- ...
- Office GVLK 密钥对照表(kms激活专用)
Office2016系列: Office Professional Plus 2016:XQNVK-8JYDB-WJ9W3-YJ8YR-WFG99 Office Standard 2016:JNRGM ...
- 如何在实际项目中使用Promise(入门级)
你们有没有遇到过这样的情况,ES6看过了,Promise的文字概念都懂,但是我要怎么在项目中去写一个Promise呢? 那天我就是带着这样的疑问去网上搜了下.最后成功地在项目中应用了Promise,只 ...
- POJ——T2271 Guardian of Decency
http://poj.org/problem?id=2771 Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 5932 A ...