二叉树的最大/小/平衡 深度 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 ...
随机推荐
- Unreal Engine 4(虚幻UE4)GameplayAbilities 插件入门教程(二)
我们接着学习.如果没有学习第一篇,请前往学习. 由于GameplayAbilities插件基本上没有资料(除了前面提供的那篇Dave的博文以外,再无资料,有迹象表明Dave是这个插件的开发者). 这个 ...
- PHP实现微信申请退款(证书权限必须设为可执行)
前期准备: 当然是搞定了微信支付,不然怎么退款,这次还是使用官方的demo.当然网上可能也有很多大神自己重写和封装了demo,或许更加好用简洁,但是我还是不提倡用,原因如下: (1)可能功能不全,或许 ...
- 试讲DOCKER专用
内容概要: DOCKER简介 为什么要用DOCKER DOCKER的应用场景 DOCKER基础 一 DOCKER简介 Docker是Docker.Inc公司开源的一个基于轻量级虚拟化技术的容器引擎项目 ...
- Hive基础之Hive是什么以及使用场景
Hive是什么1)Hive由facebook开源,构建在Hadoop (HDFS/MR)上的用于管理和查询结果化/非结构化的数据仓库:2)一种可以存储.查询和分析存储在Hadoop 中的大规模数据的机 ...
- 理解prototype
从别人的博客里面盗了2张图,这2张图将对象/实例/prototype/__proto__/constructor之间的关系描绘的很清楚. 1.prototype 为 function的属性,实例化的对 ...
- dockers的容器删除
1.停用全部运行中的容器: docker stop $(docker ps -q) 2.删除全部容器: docker rm $(docker ps -aq) 3.一条命令实现停用并删除容器: dock ...
- docker 配置pull源
登录阿里云 找到镜像服务 在/etc/docker下创建daemon.json文件并写入 最后重启docker服务 sudo service docekr restart
- 从Chrome 69.0 版本起,Flash权限受到进一步限制,默认仅在当前浏览器会话有效。
# 69.0 之后的版本 ## 从Chrome 69.0 版本起,Flash权限受到进一步限制,默认仅在当前浏览器会话有效.关闭Enable Ephemeral Flash Permissions , ...
- json小知识
json转换新方法 上面是把一个json数组转换成map集合 fromObject()是通用的 map集合,bean对象,String转换成json 文件转换成字符串,在转换成json 生成json文 ...
- CentOS 7 JDK安装
官网: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 1.下载(下图有个错误 ...