当前位置:网站首页>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.
边栏推荐
- Crawler practice website image batch download
- Short math guide for latex by Michael downs
- How to use websocket to realize simple chat function in C #
- Network byte order
- Contest3145 - the 37th game of 2021 freshman individual training match_ J: Eat radish
- LV1 tire pressure monitoring
- 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
- Zblog collection plug-in does not need authorization to stay away from the cracked version of zblog
- 2022 attached lifting scaffold worker (special type of construction work) free test questions and attached lifting scaffold worker (special type of construction work) examination papers 2022 attached
- Global and Chinese market for travel wheelchairs 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢

Tsinghua University product: penalty gradient norm improves generalization of deep learning model

Network byte order
![Backpropagation formula derivation [Li Hongyi deep learning version]](/img/ef/f76eae39c4f8716a0030a60c85b09c.gif)
Backpropagation formula derivation [Li Hongyi deep learning version]

I stepped on a foundation pit today

Gee import SHP data - crop image

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

The "message withdrawal" of a push message push, one click traceless message withdrawal makes the operation no longer difficult

Dans la recherche de l'intelligence humaine ai, Meta a misé sur l'apprentissage auto - supervisé

FRP intranet penetration

MySQL advanced SQL statement (1)
随机推荐
LV1 Roche limit
17. File i/o buffer
C language black Technology: Archimedes spiral! Novel, interesting, advanced~
No clue about the data analysis report? After reading this introduction of smartbi, you will understand!
Tsinghua University product: penalty gradient norm improves generalization of deep learning model
Format character%* s
FRP intranet penetration
MySQL workbench use
What are the main investment products of bond funds and what are they
Remote work guide
MySQL advanced SQL statement (1)
JS object definition
Hospital network planning and design document based on GLBP protocol + application form + task statement + opening report + interim examination + literature review + PPT + weekly progress + network to
String: LV1 eat hot pot
A. ABC
[Yugong series] February 2022 attack and defense world advanced question misc-83 (QR easy)
Network byte order
Network communication basic kit -- IPv4 socket structure
The "message withdrawal" of a push message push, one click traceless message withdrawal makes the operation no longer difficult
Contest3145 - the 37th game of 2021 freshman individual training match_ E: Eat watermelon