当前位置:网站首页>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.
边栏推荐
- Unity knapsack system (code to center and exchange items)
- Kiss number + close contact problem
- Node write API
- FRP intranet penetration
- Backpropagation formula derivation [Li Hongyi deep learning version]
- WordPress collection WordPress hang up collection plug-in
- No clue about the data analysis report? After reading this introduction of smartbi, you will understand!
- Buuctf QR code
- Servlet simple verification code generation
- A. Min Max Swap
猜你喜欢
(column 23) typical C language problem: find the minimum common multiple and maximum common divisor of two numbers. (two solutions)
1day vulnerability pushback skills practice (3)
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
[Yugong series] February 2022 attack and defense world advanced question misc-84 (MySQL)
Network byte order
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
No clue about the data analysis report? After reading this introduction of smartbi, you will understand!
What are the conditions for the opening of Tiktok live broadcast preview?
Jenkins continuous integration environment construction V (Jenkins common construction triggers)
ZABBIX API pulls the values of all hosts of a monitoring item and saves them in Excel
随机推荐
Global and Chinese market of small batteries 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese market of cell scrapers 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese market of box seals 2022-2028: Research Report on technology, participants, trends, market size and share
Osnabrueck University | overview of specific architectures in the field of reinforcement learning
Global and Chinese market of handheld melanoma scanners 2022-2028: Research Report on technology, participants, trends, market size and share
A. Min Max Swap
Final consistency of MESI cache in CPU -- why does CPU need cache
Global and Chinese markets of advanced X-ray inspection system (Axi) in PCB 2022-2028: Research Report on technology, participants, trends, market size and share
Tsinghua University product: penalty gradient norm improves generalization of deep learning model
Johnson–Lindenstrauss Lemma
Contest3145 - the 37th game of 2021 freshman individual training match_ G: Score
Redis transaction
Safety tips - seat belt suddenly fails to pull? High speed police remind you how to use safety belts in a standardized way
[Yugong series] February 2022 attack and defense world advanced question misc-84 (MySQL)
2022 examination summary of quality controller - Equipment direction - general basis (quality controller) and examination questions and analysis of quality controller - Equipment direction - general b
14. Process time
Practical multifunctional toolbox wechat applet source code / support traffic master
Setting methods, usage methods and common usage scenarios of environment variables in postman
Buuctf QR code
What are the conditions for the opening of Tiktok live broadcast preview?