当前位置:网站首页>104. maximum depth of binary tree
104. maximum depth of binary tree
2022-06-28 09:04:00 【Listen to my heart without asking】
Method 1 :dfs

class Solution {
public:
int maxDepth(TreeNode* root) {
if (root == nullptr) return 0;
return max(maxDepth(root->left), maxDepth(root->right)) + 1;
}
};
author :LeetCode-Solution
link :https://leetcode-cn.com/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 .

Method 2 :bfs
Using the queue
class Solution {
public:
int maxDepth(TreeNode* root) {
if (root == nullptr) return 0;
queue<TreeNode*> Q;
Q.push(root);
int ans = 0;
while (!Q.empty()) {
int sz = Q.size();
while (sz > 0) {
TreeNode* node = Q.front();Q.pop();
if (node->left) Q.push(node->left);
if (node->right) Q.push(node->right);
sz -= 1;
}
ans += 1;
}
return ans;
}
};
author :LeetCode-Solution
link :https://leetcode-cn.com/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 .

边栏推荐
- Deployment of MySQL database in Linux Environment
- APICloud携手三六零天御,助力企业守好App安全“第一关”
- containerd1.5.5的安装
- Basic content learning of software testing (I)
- 【无标题】
- 用Pytorch搭建第一个神经网络且进行优化
- Comment supprimer le crosstalk SiC MOSFET?
- Assertions used in the interface automation platform
- Common faults and solutions of Substation
- Find the total number of 1 appearing in the integer 1-N and the number of 1 in the binary of the integer
猜你喜欢

Music website design based on harmonyos (portal page)
![[untitled]](/img/bb/213f213c695795daecb81a4cf2adcd.jpg)
[untitled]

隐私计算FATE-----离线预测

Deployment of MySQL database in Linux Environment

Boundary value analysis method for learning basic content of software testing (2)
![[big case] Xuecheng online website](/img/40/beec3ba567f5a372899bb58af0d05a.png)
[big case] Xuecheng online website

Loggerfactory uses log4j Parameter introduction of properties

Installation of containerd1.5.5

Postman interface test

Expérience d'optimisation SQL: de 30248 secondes à 0001 secondes
随机推荐
如何抑制SiC MOSFET Crosstalk(串擾)?
[cloud native | kubernetes] in depth understanding of pod (VI)
Matlab tips (20) matrix analysis -- principal component regression
图解MySQL的binlog、redo log和undo log
Almost union find (weighted union search)
How to implement two factor authentication MFA based on RADIUS protocol?
Anniversary party
电子元器件销售ERP管理系统哪个比较好?
Superimposed ladder diagram and line diagram and merged line diagram and needle diagram
Maintenance and protection of common faults of asynchronous motor
买卖股票费用计算
How to solve the problem of port number occupation
DEJA_ Vu3d - 052 of cesium feature set - Simulation of satellite orbit (high altitude) effect
Error: `brew cask` is no longer a `brew` command. Use `brew <command> --cask` instead.
Is it safe to open an account for online stock speculation?
Copy & Deepcopy
Integer partition
Three body attack (three-dimensional split plus two points)
与普通探头相比,差分探头有哪些优点
Trailing Zeroes (II)