当前位置:网站首页>【LeetCode】104.二叉树的最大深度
【LeetCode】104.二叉树的最大深度
2022-08-02 02:40:00 【酥酥~】
题目
给定一个二叉树,找出其最大深度。
二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
说明: 叶子节点是指没有子节点的节点。
示例:
给定二叉树 [3,9,20,null,null,15,7],
返回它的最大深度 3 。
题解
使用递归
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */
class Solution {
public:
int fun(TreeNode* node,int high)
{
if(node==nullptr)
return high;
high++;
return max(fun(node->left,high),fun(node->right,high));
}
int maxDepth(TreeNode* root) {
int high=0;
if(root)
high = fun(root,high);
return high;
}
};
//改进
class Solution {
public:
int maxDepth(TreeNode* root) {
if(root==nullptr)
return 0;
return max(maxDepth(root->left),maxDepth(root->right))+1;
}
};
使用广度优先遍历
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */
class Solution {
public:
int maxDepth(TreeNode* root) {
if(root==nullptr)
return 0;
int high = 0;
queue<TreeNode*> myqueue;
myqueue.emplace(root);
while(!myqueue.empty())
{
high++;
queue<TreeNode*> temp;
int len = myqueue.size();
while(len>0)
{
len--;
TreeNode* node = myqueue.front();
myqueue.pop();
if(node->left)
myqueue.emplace(node->left);
if(node->right)
myqueue.emplace(node->right);
}
}
return high;
}
};
边栏推荐
- nacos startup error, the database has been configured, stand-alone startup
- AI target segmentation capability for fast video cutout without green screen
- 2022-07-30 mysql8 executes slow SQL-Q17 analysis
- Can Youxuan database import wrongly be restored?
- analog IC layout-Design for reliability
- Power button 1374. Generate each character string is an odd number
- KICAD 拉线宽度无法修改,解决方法
- 2022-08-01 Reflection
- 亲身经历过的面试题
- JS中获取对象数据类型的键值对的键与值
猜你喜欢

yaml

Chopper webshell feature analysis

Chapter 7 Noise analysis

搭建zabbix监控及邮件报警(超详细教学)

Service discovery of kubernetes

Remember a gorm transaction and debug to solve mysql deadlock

AWR analysis report questions for help: How can SQL be optimized from what aspects?

Pinduoduo leverages the consumer expo to promote the upgrading of domestic agricultural products brands and keep pace with international high-quality agricultural products

记一次gorm事务及调试解决mysql死锁
![[Server data recovery] Data recovery case of server Raid5 array mdisk disk offline](/img/08/d693c7e2fff8343b55ff3c1f9317c6.jpg)
[Server data recovery] Data recovery case of server Raid5 array mdisk disk offline
随机推荐
JS中获取对象数据类型的键值对的键与值
架构:应用架构的演进以及微服务架构的落地实践
Use DBeaver for mysql data backup and recovery
指针数组和数组指针
Flask之路由(app.route)详解
Lombok
1688以图搜货
PHP live source code to achieve simple barrage effect related code
isa指针使用详情
Safety (2)
2022-08-01 mysql/stoonedb slow SQL-Q18 analysis
IMU预积分的简单理解
analog IC layout-Environmental noise
60 Feature Engineering Operations: Using Custom Aggregate Functions【Favorites】
[Server data recovery] Data recovery case of server Raid5 array mdisk disk offline
ALCCIKERS Shane 20191114
nacos startup error, the database has been configured, stand-alone startup
记一个gorm初始化的坑
CASE2023
Can Youxuan database import wrongly be restored?