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

【MySQL从入门到精通】【高级篇】(一)字符集的修改与底层原理

Wan Weiwei, a researcher from Osaka University, Japan, introduced the rapid integration method and application of robot based on WRS system

疫情、失业,2022,我们高喊着摆烂和躺平!

ZUCC_ Principles of compiling language and compilation_ Experiment 0607 grammar analysis ll analysis

开源之夏中选名单已公示,基础软件领域成为今年的热门申请

JS to find and update the specified value in the object through the key

【量化投资】离散傅里叶变换求数组周期

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

Distributed | how to make "secret calls" with dble

rpiplay实现树莓派AirPlay投屏器
随机推荐
rpiplay实现树莓派AirPlay投屏器
Win11 blank when using VIM to view content in cmder
À propos de ETL il suffit de lire cet article, trois minutes pour vous faire comprendre ce qu'est ETL
数据中台:民生银行的数据中台实践方案
MySQL | 视图《康师傅MySQL从入门到高级》笔记
2021-05-20computed和watch应用与区别
RuntimeError: Missing dependencies:XXX
【NOI模拟赛】寄(树形DP)
ZUCC_ Principles of compiling language and compilation_ Experiment 0607 grammar analysis ll analysis
leetcode 1642. Furthest building you can reach
orb slam build bug: undefined reference to symbol ‘_ ZN5boost6system15system_ categoryEv‘
IDEA另起一行快捷键
Video Fusion communication has become an inevitable trend of emergency command communication. How to realize it based on easyrtc?
1704. 判断字符串的两半是否相似
liunx 更改 vsftpd 的端口号
Numpy 中的方法汇总
The pie chart with dimension lines can set various parameter options
【NOI模拟赛】摆(线性代数,杜教筛)
Deep learning and neural networks: the six most noteworthy trends
xargs使用技巧 —— 筑梦之路