当前位置:网站首页>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 .

边栏推荐
- Which securities company is better and safer to choose when opening an account for the inter-bank certificate of deposit fund with mobile phone
- SQL 优化经历:从 30248秒到 0.001秒的经历
- 用Pytorch搭建第一个神经网络且进行优化
- Characteristics and prevention of electrical fire
- temple
- Copy & Deepcopy
- Learn how Alibaba manages the data indicator system
- SQL injection file read / write
- Installation of containerd1.5.5
- Scenario method and error recommendation method for learning basic content of software testing (2)
猜你喜欢
![[untitled]](/img/bb/213f213c695795daecb81a4cf2adcd.jpg)
[untitled]

Error: `brew cask` is no longer a `brew` command. Use `brew <command> --cask` instead.

Basic operation of PMP from applying for the exam to obtaining the certificate, a must see for understanding PMP

Data mining modeling practice

DEJA_ Vu3d - 051 of cesium function set - perfect realization of terrain excavation

The Cassandra cluster reinstalls and starts from the node. An error is reported. There is an existing solution

Application of current limiting protector in preventing electrical fire in shopping malls

SQL 優化經曆:從 30248秒到 0.001秒的經曆
![[big case] Xuecheng online website](/img/40/beec3ba567f5a372899bb58af0d05a.png)
[big case] Xuecheng online website

基于宽表的数据建模
随机推荐
股票 停牌
理解IO模型
Apache Doris 成为 Apache 顶级项目
webrtc优势与模块拆分
Illustration of MySQL binlog, redo log and undo log
The Cassandra cluster reinstalls and starts from the node. An error is reported. There is an existing solution
Potential safety hazards in elderly care facilities
Power data
如何抑制SiC MOSFET Crosstalk(串擾)?
Super Jumping! Jumping! Jumping!
break database---mysql
Postman interface test
Common test method used by testers --- orthogonal method
Implementation of single sign on
使用transform:scale之后导致页面鼠标悬浮事件消失
How do people over 40 allocate annuity insurance? Which product is more suitable?
ffmpeg推流报错Failed to update header with correct duration.
Applet: traverse the value of an array in the list, which is equivalent to for= "list" list An item in comment
如何抑制SiC MOSFET Crosstalk(串扰)?
硬盘基本知识(磁头、磁道、扇区、柱面)