当前位置:网站首页>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;
}
}
边栏推荐
- Weight file and pre training file of yolov3
- Pytoch learning record (I): introduction to pytoch
- 统计之歌 歌词
- 二叉树——222. 完全二叉树的节点个数
- 二叉树——257. 二叉树的所有路径
- String functions and memory operation functions
- Practical experience of pair programming
- Lua script Wireshark plug-in to resolve third-party private protocols
- Leetcode169 detailed explanation of most elements
- Unexpected dubug tricks
猜你喜欢

Observer model of behavioral model

老旧笔记本电脑变服务器(笔记本电脑+内网穿透)

Interview focus - TCP protocol of transport layer

Lua脚本编写Wireshark插件解析第三方私有协议

How to use yolov5 as an intelligent transportation system for red light running monitoring (1)

The process of finding free screen recording software - I didn't expect win10 to come with this function

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

Stm32 systeminit trap during simulation debugging

Stack and queue - 239. Sliding window maximum

YoloV4-tiny网络结构
随机推荐
Typescript TS basic knowledge and so on
什么是奇偶校验?如何用C语言实现?
A brief introduction to OWASP
Leetcode169-多数元素详解
栈与队列——347. 前 K 个高频元素
多御安全浏览器手机版将增加新功能,使用户浏览更个性化
What is multithreading
Ten threats to open API ecosystem
34-SparkSQL自定义函数的使用、SparkStreaming的架构及计算流程、DStream转换操作、SparkStreaming对接kafka和offset的处理
指针函数的demo
Shardingsphere data slicing
二叉树——530.二叉搜索树的最小绝对差
@The underlying principle of Autowired annotation
Compile live555 with vs2019 in win10
Nacos 下线服务时报错 errCode: 500
Article 75: writing skills of academic papers
The GUI interface of yolov3 (3) -- solve the out of memory problem and add camera detection function
TOPSIS and entropy weight method
Annotation @autowired source code analysis
Exercise (2) create a set to store the elements "1", "$", "2", "$", "3", "$", "4"“