当前位置:网站首页>Sword finger offer 55 - I. depth DFS method of binary tree
Sword finger offer 55 - I. depth DFS method of binary tree
2022-06-24 08:52:00 【Mr Gao】
The finger of the sword Offer 55 - I. The depth of the binary tree
Enter the root node of a binary tree , Find the depth of the tree . The nodes that pass from the root node to the leaf node ( Containing root 、 Leaf nodes ) A path to a tree , The length of the longest path is the depth of the tree .
for example :
Given binary tree [3,9,20,null,null,15,7],
3
/
9 20
/
15 7
Return to its maximum depth 3 .
This question is very routine , But it is also very practical in programming , You can learn :
/** * 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);
}
边栏推荐
- Background management of uniapp hot update
- Send custom events in QT
- Database migration from PostgreSQL to MySQL
- OpenCV每日函数 结构分析和形状描述符(7) 寻找多边形(轮廓)/旋转矩形交集
- Why can ping fail while traceroute can
- GradScaler MaxClipGradScaler
- 2021-05-20computed和watch应用与区别
- 更改SSH端口号
- 原生小程序用画布制作海报,等比例缩放,和uniapp差不多就是写法有点不同
- How does the tunnel mobile inspection track robot monitor 24 hours to ensure the safety of tunnel construction?
猜你喜欢
![Jenkins is deployed automatically and cannot connect to the dependent service [solved]](/img/fe/f294955a9bdf7492aab360e44e052d.png)
Jenkins is deployed automatically and cannot connect to the dependent service [solved]

数据中台:中台架构及概述

What is SRE? A detailed explanation of SRE operation and maintenance system

关于ETL看这篇文章就够了,三分钟让你明白什么是ETL

4275. Dijkstra序列

阿里资深软件测试工程师推荐测试人员必学——安全测试入门介绍

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

【LeetCode】541. 反转字符串 II

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

【团队管理】测试团队绩效管理的25点小建议
随机推荐
【Pytorch基础教程31】YoutubeDNN模型解析
【量化投资】离散傅里叶变换求数组周期
Change SSH port number
微博撰写-流程图-序列图-甘特图-mermaid流程图-效果不错
2138. 将字符串拆分为若干长度为 k 的组
【NOI模拟赛】寄(树形DP)
Centos7 installation of jdk8, mysql5.7 and Navicat connection to virtual machine MySQL and solutions (solutions to MySQL download errors are attached)
liunx服务器 telnet 带用户名 端口登陆方法
数据中台:数据中台全栈技术架构解析,附带行业解决方案
一文讲透,商业智能BI未来发展趋势如何
What is SRE? A detailed explanation of SRE operation and maintenance system
RuntimeError: Missing dependencies:XXX
Jenkins自动化部署,连接不到所依赖的服务【已解决】
216. 组合总和 III-枚举法
How does the tunnel mobile inspection track robot monitor 24 hours to ensure the safety of tunnel construction?
Rsync for file backup
Telnet port login method with user name for liunx server
every()、map()、forEarch()方法。数组里面有对象的情况
关于ETL看这篇文章就够了,三分钟让你明白什么是ETL
数据中台:民生银行的数据中台实践方案