当前位置:网站首页>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 .
边栏推荐
- Explain the difference between void 0 and undefined
- The meaning and research significance of mathematical methodology
- Records how cookies are carried in cross domain requests
- Post exam summary
- Promises/a+ standard Chinese Translation
- Aircraft war from scratch (II) simple development
- Common troubleshooting tools and analysis artifacts are worth collecting
- 顶流编辑器 Atom,将于 12 月 15 日退出历史舞台
- WPF data binding (IV)
- VTK vtkplane and vtkcutter use
猜你喜欢

品牌定位个性六种形态及结论的重大意义
![[Xunwei dry goods] opencv test of Godson 2k1000 development board](/img/94/312bb1f0d5e8d49506f659ad23cd3a.jpg)
[Xunwei dry goods] opencv test of Godson 2k1000 development board

Starting from scratch (V) realize bullet positioning and animation

VTK-vtkPlane和vtkCutter使用

saltstack部署lnmp

无心剑汉英双语诗001.《爱》

教育专家王中泽老师多年经验分享:家庭教育不是附庸品

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

Shuttle container component

Esp32 learning notes (49) - esp-wifi-mesh interface use
随机推荐
Common troubleshooting tools and analysis artifacts are worth collecting
Cv2.rectangle() picture frame
Dynamically change the direction of this
Prototype and prototype chain
SQL query. Only the column name is displayed but not the data
Analysis of key points and difficulties of ES6 promise source code
About the designer of qtcreator the solution to the problem that qtdesigner can't pull and hold controls normally
Comparison of DOM tags of wechat applet development (native and uniapp)
教育专家王中泽老师一招解决学生问题
迅为干货 |瑞芯微RK3568开发板TFTP&NFS烧写(上)
Error occurred in pycharm DeprecatedEnv: Env FrozenLake-v0 not found (valid versions include [‘FrozenLake-v1‘])
一、SQLServer2008安装(带密码)、创建数据库、C#窗体项目测试
Records how cookies are carried in cross domain requests
[deploy private warehouse based on harbor] 4 push image to harbor
Aircraft battle from scratch (III) flight between player aircraft and enemy aircraft
LEARNING TARGET-ORIENTED DUAL ATTENTION FOR ROBUST RGB-T TRACKING
洛谷P1091合唱队形(最长上升子序列)
常用问题排查工具和分析神器,值得收藏
Whether the ZABBIX monitoring host is online
Object. Specific implementation and difference between create() and new