当前位置:网站首页>Binary tree -- 104. Maximum depth of binary tree
Binary tree -- 104. Maximum depth of binary tree
2022-07-26 00:04:00 【Xiao Zhao, who is working hard for millions of annual salary】
1 Title Description
Given a binary tree , Find out the maximum depth .
The depth of a binary tree is the number of nodes in the longest path from the root node to the farthest leaf node .
explain : A leaf node is a node that has no children .
2 Title Example

3 Topic tips
There is no hint in this question
4 Ideas
Method 1 : Depth-first search
If we know the maximum depth of the left and right subtrees Ⅰ and r, Then the maximum depth of the binary tree is
max(l, r)+1
The maximum depth of left and right subtrees can be calculated in the same way . So we can use 「 Depth-first search 」 To calculate the maximum depth of the binary tree . To be specific , When calculating the maximum depth of the current binary tree , You can recursively calculate the maximum depth of its left and right subtrees , And then in O(1) Calculate the maximum depth of the current binary tree in time . Recursion exits when an empty node is accessed .
Complexity analysis
Time complexity :O(n), among n Is the number of binary tree nodes . Each node is recursively traversed only once .
Spatial complexity :O(height), among height Represents the height of the binary tree . Recursive functions require stack space , And stack space depends on the depth of recursion , So the space complexity is equivalent to the height of the binary tree .
Method 2 : Breadth first search
We can also use 「 Breadth first search 」 To solve this problem , But we need to — Some modifications , At this time, our breadth first search queue is 「 All nodes of the current layer 」. Every time you expand to the next level , Unlike breadth first search, only one node is taken out of the queue at a time , We need to expand all nodes in the queue , This ensures that all nodes of the current layer are stored in the queue every time the expansion is completed , That is, we expand layer by layer , Finally, we use a variable ans To maintain the number of expansions , The maximum depth of the binary tree is ans.
Complexity analysis
· Time complexity :O(n), among n Is the number of nodes in a binary tree . The same analysis as method 1 , Each node is accessed only once .
· Spatial complexity : The consumption of this method space depends on the number of elements stored in the queue , It will reach... In the worst case O(n).
5 My answer
Method 1 : Depth-first search
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;
}
}
}
Method 2 : Breadth first search
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;
}
}
边栏推荐
- 二叉树——110. 平衡二叉树
- Bubble sort idea and Implementation
- Lua脚本编写Wireshark插件解析第三方私有协议
- The GUI interface of yolov3 (3) -- solve the out of memory problem and add camera detection function
- SQLZOO——Nobel Quiz
- 二叉树相关知识
- Cherish time and improve efficiency
- String functions and memory operation functions
- 模块二作业
- Nacos 下线服务时报错 errCode: 500
猜你喜欢

Program environment and pretreatment

Android solves the risk of database injection vulnerability

Sequence traversal II of leetcode107 binary tree

什么叫做 inode ?带你理解 inode 和对于创建文件和删除文件时 inode 都提供了哪些帮助。

SIGIR‘22 推荐系统论文之图网络篇

String functions and memory operation functions

C语言实战之猜拳游戏

Lua script Wireshark plug-in to resolve third-party private protocols

Stm32 systeminit trap during simulation debugging
34-SparkSQL自定义函数的使用、SparkStreaming的架构及计算流程、DStream转换操作、SparkStreaming对接kafka和offset的处理
随机推荐
Song of statistics lyrics
二叉树——112. 路径总和
Prometheus operation and maintenance tool promtool (II) query function
本轮牛市还能持续多久?|疑问解答 2021-05-11
ShardingSphere数据分片
Qt智能指针易错点
Leetcode169-多数元素详解
Yolov4 tiny network structure
Redis extended data type (jump table /bitmaps/hyperloglog/geospatial)
二叉树——617. 合并二叉树
注解@Autowired源码解析
C# - readonly 和 const 关键字
@The underlying principle of Autowired annotation
STM32 lighting procedure
Exercise (1) create a set C1 to store the elements "one", "two", "three"“
What is inode? It will help you understand inode and what help inode provides when creating and deleting files.
MySQL的DDL、DML和DQL的基本语法
QT smart pointer error prone point
Program environment and pretreatment
Pyqt5 rapid development and actual combat.pdf sharing