二叉树的最大/小/平衡 深度 depth of binary tree
[抄题]:
给定一个二叉树,找出其最大深度。
二叉树的深度为根节点到最远叶子节点的距离。
[思维问题]:
[一句话思路]:
分合法的定义
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
求最小距离时,注意左边或右边没有数的情况。
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[总结]:
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[英文数据结构,为什么不用别的数据结构]:
只有算法,没有数据结构
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
public class Solution {
/**
* @param root: The root of binary tree.
* @return: An integer.
*/
public int maxDepth(TreeNode root) {
if (root == null) {
return 0;
} int left = maxDepth(root.left);
int right = maxDepth(root.right); int max = Math.max(left,right);
return max + 1;
}
}
public class Solution {
/**
* @param root: The root of binary tree.
* @return: An integer.
*/
public int minDepth(TreeNode root) {
if (root == null) {
return 0;
} int left = minDepth(root.left);
int right = minDepth(root.right);
int result;
int min = Math.min(left,right); if (left == 0 || right == 0) {
result = left + right + 1;
}
else {
result = min + 1;
}
return result;
}
}
二叉树的最大/小/平衡 深度 depth of binary tree的更多相关文章
- (二叉树 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 ...
- (二叉树 BFS DFS) leetcode 111. Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- [LeetCode] Minimum Depth of Binary Tree 二叉树的最小深度
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- LeetCode:111_Minimum Depth of Binary Tree | 二叉树的最小深度 | Easy
要求:此题正好和Maximum Depth of Binary Tree一题是相反的,即寻找二叉树的最小的深度值:从根节点到最近的叶子节点的距离. 结题思路:和找最大距离不同之处在于:找最小距离要注意 ...
- [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)
[Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...
- [LeetCode] 111. Minimum Depth of Binary Tree 二叉树的最小深度
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- leetcode 104 Maximum Depth of Binary Tree二叉树求深度
Maximum Depth of Binary Tree Total Accepted: 63668 Total Submissions: 141121 My Submissions Question ...
- [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 ...
随机推荐
- python中将HTTP头部中的GMT时间转换成datetime时间格式
原文: https://blog.csdn.net/zoulonglong/article/details/80585716 需求背景:目前在做接口的自动化测试平台,由于接口用例执行后返回的结果中的时 ...
- [UE4]为UStaticMeshComponent添加点击事件
BlockMesh->OnClicked.AddDynamic(this, &APuzzleBlock::BlockClicked); //鼠标点击事件 BlockMesh->On ...
- Ubuntu14.04下hadoop-2.6.0单机配置和伪分布式配置
需要重新编译的教程:http://blog.csdn.net/ggz631047367/article/details/42460589 在Ubuntu下创建hadoop用户组和用户 hadoop的管 ...
- mysql5.6修改字符编码,ERR:Illegal mix of collations for operation 'concat'
mysql5.6修改字符编码,ERR:Illegal mix of collations for operation 'concat' 1.问题起因:搭建环境初始化mysql的时候看到mysql配置文 ...
- 63. sqlserver查版本号问题
SELECT @@VERSION as 版本详细情况 SELECT SERVERPROPERTY('edition') as 软件版本 SELECT SERVERPROPERTY('ProductVe ...
- el 表达式的比较和包含
相等( equal ) :eq 不相等( not equal ): ne / neq 大于( greater than ): gt 小于( less than ): lt 大于等于( great th ...
- Linux tomcat启动慢, Creation of SecureRandom instance for session ID generation using [SHA1PRNG]took [xx] mil
启动慢的解决链接: http://blog.csdn.net/u011627980/article/details/54024974
- sqlserver还原数据库
该方法只针对同等级数据库,不能跨级 比如sqlserver2012还原到sqlserver2008会报错 用数据库日志文件对数据库进行还原一 将日志文件.mdf文件和.ldf文件copy放置在sq ...
- SpringMVC知识(1)
1.SpringMVC的工作流程 流程 : 1.用户发送请求至前端控制器DispatcherServlet 2.DispatcherServlet收到请求调用HandlerMapping处理器映射器. ...
- 安卓上为什么不能用system.io.file读取streammingAssets目录下的文件
首先,看文档: Streaming Assets Most assets in Unity are combined into the project when it is built. Howe ...