当前位置:网站首页>Leetcode 104. Maximum depth of binary tree
Leetcode 104. Maximum depth of binary tree
2022-07-22 02:41:00 【LuZhouShiLi】
Leetcode 104. The maximum depth of a binary tree
subject
Ideas
- Determine the parameters and return values of recursive functions , The parameter is the root node of the binary tree , The return value is the depth of the tree
- Recursive export , Empty node , return 0
- Determine single level recursive logic : First find the depth of the left subtree , Then find the depth of the right subtree , Then take the maximum depth plus one , Is the maximum depth of the current node
Code
/** * 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 getDepth(TreeNode* root)
{
if(root == NULL)
{
return 0;// Recursive export
}
int leftDepth = getDepth(root->left);
int rightDepth = getDepth(root->right);
int depth = max(leftDepth,rightDepth) + 1;
return depth;
}
int maxDepth(TreeNode* root) {
return getDepth(root);
}
};
边栏推荐
- 电脑是怎样上网的 (三) 报文头封装和接入网与网络运营商
- [H3C device networking configuration]
- 【开发教程4】疯壳·人形街舞机器人-PC 上位机在线调试
- Precautions for selecting WMS storage barcode management system
- Qt | Qt的项目文件.pro文件详解
- 上海地铁9号线有人持刀砍人?轨交公安:是安全演练
- Fluent introduces the graphic verification code and retains the sessionid
- leetcode:169. 多数元素
- [Development Tutorial 4] crazy shell · humanoid hip-hop robot PC upper computer online debugging
- 显示一个圆,并可以移动
猜你喜欢
别乱用UUID了,自增ID和UUID性能差距你测试过吗?
Sentinel theoretical knowledge and introduction
Creation of configuration files in Nacos configuration center and reading of Nacos configuration center by microservices
un7.20:如何同时将关联表中的两个属性展示出来?
thymeleaf应用笔记
交换机基本原理与配置
6. < tag dynamic planning and housebreaking collection (tree DP) > lt.198 Home raiding + lt.213 Looting II + lt.337 Looting III DBC
SQL basic statement exercise
[intensive reading of thesis]attention is all you need
基于JSP实现OA办公系统
随机推荐
Ali Er Mian: what is MMAP? (not MMP)
电脑是怎样上网的 (二) 从网线到网络设备
Creation of configuration files in Nacos configuration center and reading of Nacos configuration center by microservices
辛丑年之万家灯火
Precautions for selecting WMS storage barcode management system
Use dichotomy to find peak value
Ancient Chinese civilization
电脑是怎样上网的 (一) 消息产生 DNS
【开发教程4】疯壳·人形街舞机器人-PC 上位机在线调试
Leetcode 111. 二叉树的最小深度
Flink应用案例统计实现TopN的两种方式
SQL每日一练(牛客新题库)——第3天: 条件查询
阿里云技术专家杨泽强:弹性计算云上可观测能力构建
With a wave of operations, I have improved the efficiency of SQL execution by 10000000 times
【sklearn】数据集拆分 sklearn.moduel_selection.train_test_split
FastDFS高可用使用介绍
Two ways to implement topn with Flink application case statistics
交换机基本原理与配置
静态路由原理与配置
Distributed transaction two-phase commit, at mode, TCC mode