当前位置:网站首页>剑指 Offer 55 - I. 二叉树的深度-dfs法
剑指 Offer 55 - I. 二叉树的深度-dfs法
2022-06-24 07:07:00 【Mr Gao】
剑指 Offer 55 - I. 二叉树的深度
输入一棵二叉树的根节点,求该树的深度。从根节点到叶节点依次经过的节点(含根、叶节点)形成树的一条路径,最长路径的长度为树的深度。
例如:
给定二叉树 [3,9,20,null,null,15,7],
3
/
9 20
/
15 7
返回它的最大深度 3 。
这题很常规,但是编程中也很实用,可以学习学习:
/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */
int f(struct TreeNode* root){
if(root){
int a=f(root->left)+1;
int b=f(root->right)+1;
if(a>b){
return a;
}
else{
return b;
}
}
else{
return 0;
}
}
int maxDepth(struct TreeNode* root){
return f(root);
}
边栏推荐
- Common CVM transcribes audio using virtual sound card
- Why do you want to file? What are the advantages and disadvantages of website filing?
- RuntimeError: Missing dependencies:XXX
- 【NOI模拟赛】给国与时光鸡(构造)
- Redis cluster data skew
- Variable declaration and some special variables in shell
- À propos de ETL il suffit de lire cet article, trois minutes pour vous faire comprendre ce qu'est ETL
- Tencent conference API - get rest API & webhook application docking information
- Several schemes of PHP code encryption
- 数据中台:数据中台全栈技术架构解析,附带行业解决方案
猜你喜欢

表单图片上传在Chorme中无法查看请求体的二进制图片信息

数据库迁移从PostgreSQL迁移到 MYSQL

What is the future development trend of Business Intelligence BI

Xiaohei ai4code code baseline nibble 1

Redis cluster data skew

À propos de ETL il suffit de lire cet article, trois minutes pour vous faire comprendre ce qu'est ETL

【关于运维和网工的差别,一文说透】

Pymysql inserts data into MySQL and reports an error for no reason

Liunx Mysql安装

关于ETL看这篇文章就够了,三分钟让你明白什么是ETL
随机推荐
原生小程序用画布制作海报,等比例缩放,和uniapp差不多就是写法有点不同
Shell basic operators -- relational operators
2022.06.23(LC_144,94,145_二叉树的前序、中序、后序遍历)
Easydss anonymous live channel data volume instability optimization scheme sharing
Glusterfs replacement failure brick
Easycvr invokes the interface parameter acquisition method and precautions of device video recording on the page
Use cpulimit to free up your CPU
【团队管理】测试团队绩效管理的25点小建议
Common CVM transcribes audio using virtual sound card
There was an error checking the latest version of pip
Why do you want to file? What are the advantages and disadvantages of website filing?
Common misconceptions in Tencent conference API - signature error_ code 200003
Qt源码分析--QObject(2)
Centos7 installation of jdk8, mysql5.7 and Navicat connection to virtual machine MySQL and solutions (solutions to MySQL download errors are attached)
New technology practice, encapsulating the permission application library step by step with the activity results API
2021-05-20computed和watch应用与区别
第七章 操作位和位串(三)
Win10 cloud, add Vietnamese
Scheduled database backup script
图片工具