当前位置:网站首页>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.
边栏推荐
- Remote work guide
- Sword finger offer 14- I. cut rope
- Global and Chinese market of thin film deposition systems 2022-2028: Research Report on technology, participants, trends, market size and share
- Question C: Huffman tree
- Key knowledge of C language
- 在尋求人類智能AI的過程中,Meta將賭注押向了自監督學習
- The requests module uses
- 14. Process time
- ZABBIX API batch delete a template of the host
- Résumé: entropie, énergie libre, symétrie et dynamique dans le cerveau
猜你喜欢

Unity knapsack system (code to center and exchange items)

Pagoda SSL can't be accessed? 443 port occupied? resolvent

C learning notes: C foundation - Language & characteristics interpretation

What is cloud primordial?

Package and download 10 sets of Apple CMS templates / download the source code of Apple CMS video and film website

How to subcontract uniapp and applet, detailed steps (illustration) # yyds dry goods inventory #

Www 2022 | taxoenrich: self supervised taxonomy complemented by Structural Semantics

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

Comment la transformation numérique du crédit d'information de la Chine passe - t - elle du ciel au bout des doigts?

Save Private Ryan - map building + voltage dp+deque+ shortest circuit
随机推荐
Mysql-15 aggregate function
Leetcode 110 balanced binary tree
Global and Chinese market of thin film deposition systems 2022-2028: Research Report on technology, participants, trends, market size and share
Pagoda SSL can't be accessed? 443 port occupied? resolvent
Unspeakable Prometheus monitoring practice
A. Min Max Swap
Libcblas appears when installing opencv import CV2 so. 3:cannot open shared object file:NO such file or directory
2022 examination summary of quality controller - Equipment direction - general basis (quality controller) and examination questions and analysis of quality controller - Equipment direction - general b
1day vulnerability pushback skills practice (3)
Node write API
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
Optimization theory: definition of convex function + generalized convex function
Setting methods, usage methods and common usage scenarios of environment variables in postman
Design and implementation of redis 7.0 multi part AOF
The 37 year old programmer was laid off, and he didn't find a job for 120 days. He had no choice but to go to a small company. As a result, he was confused
Final consistency of MESI cache in CPU -- why does CPU need cache
[untitled] the relationship between the metauniverse and digital collections
String: LV1 eat hot pot
On Valentine's day, I code a programmer's exclusive Bing Dwen Dwen (including the source code for free)
Contest3145 - the 37th game of 2021 freshman individual training match_ G: Score