当前位置:网站首页>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);
}
边栏推荐
猜你喜欢

Centos7 installation of jdk8, mysql5.7 and Navicat connection to virtual machine MySQL and solutions (solutions to MySQL download errors are attached)

K8S部署高可用postgresql集群 —— 筑梦之路
![[pytorch basic tutorial 30] code analysis of DSSM twin tower model](/img/4a/1f290990b39c517efb2b9cef0b5fcb.png)
[pytorch basic tutorial 30] code analysis of DSSM twin tower model

4275. Dijkstra序列

Xiaohei ai4code code baseline nibble 1

【LeetCode】387. 字符串中的第一个唯一字符
![打印出来的对象是[object object],解决方法](/img/fc/9199e26b827a1c6304fcd250f2301e.png)
打印出来的对象是[object object],解决方法

Send custom events in QT

What is the future development trend of Business Intelligence BI

It is enough to read this article about ETL. Three minutes will let you understand what ETL is
随机推荐
教程篇(5.0) 08. Fortinet安全架构集成与FortiXDR * FortiEDR * Fortinet 网络安全专家 NSE 5
Mysql数据(Liunx环境)定时备份
Solving linear equations with MATLAB ax=b
不能改变虚拟机电源状态报错解决方案
Double pointer analog
数据中台:中台实践与总结
开源之夏中选名单已公示,基础软件领域成为今年的热门申请
【NOI模拟赛】摆(线性代数,杜教筛)
rpiplay实现树莓派AirPlay投屏器
Matlab camera calibrator camera calibration
Camera projection matrix calculation
input的聚焦后的边框问题
Summary of methods in numpy
打印出来的对象是[object object],解决方法
解决:模型训练时loss出现nan
À propos de ETL il suffit de lire cet article, trois minutes pour vous faire comprendre ce qu'est ETL
什么是SRE?一文详解SRE运维体系
every()、map()、forEarch()方法。数组里面有对象的情况
【Pytorch基础教程31】YoutubeDNN模型解析
QT source code analysis -- QObject (2)