当前位置:网站首页>【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;
}
};
边栏推荐
- 记一次gorm事务及调试解决mysql死锁
- EasyGBS平台播放视频时偶尔出现播放失败是什么原因?
- Entry name 'org/apache/commons/codec/language/bm/gen_approx_greeklatin.txt' collided
- 790. 数的三次方根
- 29. 删除链表中重复的节点
- BI-SQL丨WHILE
- NIO's Sword
- Rasa 3 x learning series - Rasa - 4873 dispatcher Issues. Utter_message study notes
- 十字光标太小怎么调节、CAD梦想画图算量技巧
- Nanoprobes Polyhistidine (His-) Tag: Recombinant Protein Detection Protocol
猜你喜欢
最大层内元素和
ros多客户端请求服务
【Unity入门计划】2D Game Kit:初步了解2D游戏组成
四元数、罗德里格斯公式、欧拉角、旋转矩阵推导和资料
数仓:为什么说 ETL 的未来不是 ELT,而是 EL (T)
EasyGBS平台播放视频时偶尔出现播放失败是什么原因?
罗德里格斯公式(Rodrigues‘ Rotation Formula)推导
Good News | AR opens a new model for the textile industry, and ALVA Systems wins another award!
因为WiFi原因navicat 无法连接数据库Mysql
The state status is displayed incorrectly after the openGauss switch
随机推荐
2022-08-01 Reflection
亲身经历过的面试题
Qt自定义控件和模板分享
51. 数字排列
Good News | AR opens a new model for the textile industry, and ALVA Systems wins another award!
What to study after the PMP exam?The soft exam ahead is waiting for you~
OC中new和init的区别
C#测试项目中属性的用法
29. 删除链表中重复的节点
微服务:微智能在软件系统的简述
Safety (1)
忽晴忽雨
架构:微服务网关(SIA-Gateway)简介
How engineers treat open source
数仓:数仓从ETL到ELT架构的转化以及俩者的区别
NIO's Sword
2022-07-30 mysql8 executes slow SQL-Q17 analysis
Outsourcing worked for three years, it was abolished...
Install mysql using docker
svm.SVC应用实践1--乳腺癌检测