当前位置:网站首页>Leetcode-104. Maximum Depth of Binary Tree
Leetcode-104. Maximum Depth of Binary Tree
2022-06-11 07:06:00 【Eistert】
subject
Given the root of a binary tree, return its maximum depth.
A binary tree’s maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
Example 1:
Input: root = [3,9,20,null,null,15,7]
Output: 3
Example 2:
Input: root = [1,null,2]
Output: 2
Constraints:
- The number of nodes in the tree is in the range [0, 104].
- -100 <= Node.val <= 100
solution
Method 1 : Depth-first search
Code
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */
class Solution {
public int maxDepth(TreeNode root) {
if(root == null){
return 0;
}
return 1 + Math.max(maxDepth(root.left),maxDepth(root.right));
}
}
Complexity analysis
- Time complexity :O(n);
- Spatial complexity :O(height);
Method 2 : Breadth first search
We can also use 【 Breadth first search 】 To solve this problem , But we need to make some changes , At this point, 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.
Code
public int maxDepth(TreeNode root) {
if (root == null) {
return 0;
}
Queue<TreeNode> queue = new LinkedList<TreeNode>();
// Add the root node to the queue
queue.offer(root);
// The initial result is 0
int ans = 0;
while (!queue.isEmpty()) {
// The number of nodes in the current layer
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--;
}
// The number of layers plus 1
ans++;
}
return ans;
}
Complexity analysis
- Time complexity :O(n);
- Spatial complexity :O(height);
Reprint
author :LeetCode-Solution
link :https://leetcode.cn/problems/maximum-depth-of-binary-tree/solution/er-cha-shu-de-zui-da-shen-du-by-leetcode-solution/
source : Power button (LeetCode)
The copyright belongs to the author . Commercial reprint please contact the author for authorization , Non-commercial reprint please indicate the source .
边栏推荐
- Education expert wangzhongze solves students' problems with one move
- Listen to the left width of the browser to calculate the distance
- Xunwei dry goods | Ruixin micro rk3568 development board TFTP & NFS writing (Part 1)
- Practice: how to reasonably design functions to solve practical problems in software development (I)
- Flutter 内外边距
- Leetcode hot topic 100 topic 6-10 solution
- A highly controversial issue
- byte和bit的区别
- 【Matlab图像加密解密】混沌序列图像加密解密(含相关性检验)【含GUI源码 1862期】
- 1266_ Implementation analysis of FreeRTOS scheduler startup code
猜你喜欢
![pycharm出现error.DeprecatedEnv: Env FrozenLake-v0 not found (valid versions include [‘FrozenLake-v1‘])](/img/1c/4013479ce1fc5b0ff2ebeb754f05a9.png)
pycharm出现error.DeprecatedEnv: Env FrozenLake-v0 not found (valid versions include [‘FrozenLake-v1‘])

Education expert wangzhongze shared his experience for many years: family education is not a vassal

News web page display
![[deploy private warehouse based on harbor] 4 push image to harbor](/img/af/8e28b229d94f3e6eab02308b69dc74.jpg)
[deploy private warehouse based on harbor] 4 push image to harbor

webserver

client-go gin的简单整合六-list-watch二(关于Rs与Pod以及Deployment的完善)

Detailed explanation of mutationobserver

The difference between arrow function and ordinary function

Esp32 learning notes (49) - esp-wifi-mesh interface use

latex 各种箭头/带文字标号的箭头/可变长箭头
随机推荐
Leetcode hot topic 100 topic 21-25 solution
WPF 数据绑定(四)
pycharm出现error.DeprecatedEnv: Env FrozenLake-v0 not found (valid versions include [‘FrozenLake-v1‘])
Heartless sword Chinese English bilingual poem 001 Love
商汤科技积极复工,将大力投入数字哨兵的产能和部署
Nodejs database (Part 2)
教育专家王中泽老师一招解决学生问题
【LeetCode】-- 17.电话号码的字母组合
Dynamically change the direction of this
Shutter restraint container assembly
服务器调参实录
Reconstruction and preheating of linked list information management system (2) how to write the basic logic using linear discontinuous structure?
Cross-Modal Pattern-Propagation for RGB-T Tracking
How to make time planning
[MATLAB image fusion] particle swarm optimization adaptive multispectral image fusion [including source code phase 004]
【Matlab图像融合】粒子群优化自适应多光谱图像融合【含源码 004期】
Leetcode hot topic 100 topic 6-10 solution
client-go gin的简单整合六-list-watch二(关于Rs与Pod以及Deployment的完善)
Post exam summary
Oracle pl/sql these query results cannot be updated. Please include ROWID or use Select For update