当前位置:网站首页>二叉树——104. 二叉树的最大深度
二叉树——104. 二叉树的最大深度
2022-07-25 23:55:00 【向着百万年薪努力的小赵】
1 题目描述
给定一个二叉树,找出其最大深度。
二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
说明: 叶子节点是指没有子节点的节点。
2 题目示例

3 题目提示
该题竟然没有任何提示
4 思路
方法一:深度优先搜索
如果我们知道了左子树和右子树的最大深度Ⅰ和r,那么该二叉树的最大深度即为
max(l, r)+1
而左子树和右子树的最大深度又可以以同样的方式进行计算。因此我们可以用「深度优先搜索」的方法来计算二叉树的最大深度。具体而言,在计算当前二叉树的最大深度时,可以先递归计算出其左子树和右子树的最大深度,然后在O(1)时间内计算出当前二叉树的最大深度。递归在访问到空节点时退出。
复杂度分析
时间复杂度:O(n),其中n为二叉树节点的个数。每个节点在递归中只被遍历一次。
空间复杂度:O(height),其中height表示二叉树的高度。递归函数需要栈空间,而栈空间取决于递归的深度,因此空间复杂度等价于二叉树的高度。
方法二:广度优先搜索
我们也可以用「广度优先搜索」的方法来解决这道题目,但我们需要对其进行—些修改,此时我们广度优先搜索的队列里存放的是「当前层的所有节点」。每次拓展下一层的时候,不同于广度优先搜索的每次只从队列里拿出一个节点,我们需要将队列里的所有节点都拿出来进行拓展,这样能保证每次拓展完的时候队列里存放的是当前层的所有节点,即我们是一层一层地进行拓展,最后我们用一个变量ans来维护拓展的次数,该二叉树的最大深度即为ans。
复杂度分析
·时间复杂度:O(n),其中n为二叉树的节点个数。与方法一同样的分析,每个节点只会被访问一次。
·空间复杂度:此方法空间的消耗取决于队列存储的元素数量,其在最坏情况下会达到O(n)。
5 我的答案
方法一:深度优先搜索
class Solution {
public int maxDepth(TreeNode root) {
if (root == null) {
return 0;
} else {
int leftHeight = maxDepth(root.left);
int rightHeight = maxDepth(root.right);
return Math.max(leftHeight, rightHeight) + 1;
}
}
}
方法二:广度优先搜索
class Solution {
public int maxDepth(TreeNode root) {
if (root == null) {
return 0;
}
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.offer(root);
int ans = 0;
while (!queue.isEmpty()) {
int size = queue.size();
while (size > 0) {
TreeNode node = queue.poll();
if (node.left != null) {
queue.offer(node.left);
}
if (node.right != null) {
queue.offer(node.right);
}
size--;
}
ans++;
}
return ans;
}
}
边栏推荐
- Anti shake and throttling
- What is the difference between'1 and'b1 when assigning values
- 行为型模式之迭代器模式
- Static agent + dynamic agent
- S4/hana ME21N create Po output control message button missing solution (switch EDI output mode brf+ to Nast mode)
- How to solve cross domain problems
- Yolov3 trains its own data set
- 疫情之下的好消息
- Problem set
- Payment terms in SAP message No. vg202 IDoc e1edk18 have been transferred: check data
猜你喜欢

initializer_ List tool library learning

Leetcode 0135. distribute candy

redis-基本数据类型(String/list/Set/Hash/Zset)

S4/hana mm & SD EDI Nast based integrated configuration (orders, ordrsp, desadv, invoice)

TOPSIS and entropy weight method

R language installation tutorial | graphic introduction is super detailed

Swap, move, forward, exchange of utility component learning

Redis basic data type (string/list/set/hash/zset)

反射之类加载过程

什么叫做 inode ?带你理解 inode 和对于创建文件和删除文件时 inode 都提供了哪些帮助。
随机推荐
调用钉钉api报错:机器人发送签名过期;solution:签名生成时间和发送时间请保持在 timestampms 以内
Part 66: monocular 3D reconstruction point cloud
Array merge method: concat()
The difference between SFTP and FTP
Seventy second: pedestrian detection
VSCode格式化Json文件
Lua script Wireshark plug-in to resolve third-party private protocols
QT smart pointer error prone point
Dead letter queue and message TTL expiration code
[learning notes] unreal 4 engine introduction (III)
Good news under the epidemic
【ManageEngine】ServiceDesk Plus荣获2022安全样板工程数据安全奖
程序环境和预处理
Redis basic data type (string/list/set/hash/zset)
Practical experience of pair programming
Data intensive application system design - Application System Overview
《数据密集型应用系统设计》 - 应用系统概览
Article 75: writing skills of academic papers
Graph traversal DFS, BFS (code explanation)
Yolov3 trains its own data set