当前位置:网站首页>Sword finger offer:55 - I. depth of binary tree
Sword finger offer:55 - I. depth of binary tree
2022-07-04 02:54:00 【Village Yuyao】
1. subject
The finger of the sword Offer 55 - I. The depth of the binary tree
2. describe
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 .
Tips :
Total number of nodes <= 10000
3. Implementation method
3.1 Method 1
3.1.1 Ideas
- First determine whether the root node is
null, Yes, the depth is 0; - Then traverse the larger depth in the left and right subtrees , And then add 1 ( The root node ) Is the depth of the binary tree ;
- Because you need to traverse all nodes , So the time complexity is O(n);
3.1.2 Realization
public int maxDepth(TreeNode root) {
if (root == null) {
return 0;
}
return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
边栏推荐
- Experience summary of the 12th Blue Bridge Cup (written for the first time)
- 機器學習基礎:用 Lasso 做特征選擇
- Lichuang EDA learning notes 14: PCB board canvas settings
- Setting methods, usage methods and common usage scenarios of environment variables in postman
- Site favorites
- The difference between int (1) and int (10)
- Properties of binary trees (numerical aspects)
- Solve the problem that the tabbar navigation at the bottom of vantui does not correspond to the page (window.loading.hash)
- Safety tips - seat belt suddenly fails to pull? High speed police remind you how to use safety belts in a standardized way
- Contest3145 - the 37th game of 2021 freshman individual training match_ J: Eat radish
猜你喜欢

Bugku Zhi, you have to stop him

7 * 24-hour business without interruption! Practice of applying multiple live landing in rookie villages

Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?

ZABBIX API pulls the values of all hosts of a monitoring item and saves them in Excel

Jenkins continuous integration environment construction V (Jenkins common construction triggers)

Contest3145 - the 37th game of 2021 freshman individual training match_ 1: Origami

Network byte order
![[Yugong series] February 2022 attack and defense world advanced question misc-84 (MySQL)](/img/4d/4a6b76896bf81a27d036a047f68b2b.jpg)
[Yugong series] February 2022 attack and defense world advanced question misc-84 (MySQL)

150 ppt! The most complete "fair perception machine learning and data mining" tutorial, Dr. AIST Toshihiro kamishima, Japan

MySQL workbench use
随机推荐
WP collection plug-in free WordPress collection hang up plug-in
The "message withdrawal" of a push message push, one click traceless message withdrawal makes the operation no longer difficult
Osnabrueck University | overview of specific architectures in the field of reinforcement learning
Take you to master the formatter of visual studio code
LV1 Roche limit
Basé sur... Netcore Development blog Project Starblog - (14) Implementation of theme switching function
Yyds dry goods inventory override and virtual of classes in C
Lichuang EDA learning notes 14: PCB board canvas settings
Imperial cms7.5 imitation "D9 download station" software application download website source code
JS object definition
1day vulnerability pushback skills practice (3)
How to use STR function of C language
Johnson–Lindenstrauss Lemma
C learning notes: C foundation - Language & characteristics interpretation
No clue about the data analysis report? After reading this introduction of smartbi, you will understand!
Amélioration de l'efficacité de la requête 10 fois! 3 solutions d'optimisation pour résoudre le problème de pagination profonde MySQL
Design and implementation of redis 7.0 multi part AOF
The "two-way link" of pushing messages helps app quickly realize two-way communication capability
ZABBIX API pulls the values of all hosts of a monitoring item and saves them in Excel
Record a problem that soft deletion fails due to warehouse level error