当前位置:网站首页>Sword finger offer 55 - I. depth of binary tree
Sword finger offer 55 - I. depth of binary tree
2022-07-07 22:52:00 【Yes' level training strategy】
subject : 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],
Return to its maximum depth 3 .
The time for the person who will write this topic is estimated to be a few seconds .
It's actually intuitive , Want to calculate the depth of the tree , The direct idea is DFS, and DFS The realization of is to use recursion to realize perfectly .
The condition of recursive jump is very simple root == null
, There is no doubt about this .
If not equal to null, Then the current node is a depth , Then add the depth of the larger of the two subtrees , Is the maximum depth of a real tree .
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */
class Solution {
public int maxDepth(TreeNode root) {
if(root == null) {
return 0;
}
return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
}
}
A simple question .
https://leetcode-cn.com/problems/er-cha-shu-de-shen-du-lcof
边栏推荐
- Unity technical notes (I) inspector extension
- 全面掌控!打造智慧城市建设的“领导驾驶舱”
- 0-5vac to 4-20mA AC current isolated transmitter / conversion module
- Pyqt GUI interface and logic separation
- Amesim2016 and matlab2017b joint simulation environment construction
- Redis集群安装
- Record problems fgui tween animation will be inexplicably killed
- Leetcode94. Middle order traversal of binary trees
- Matplotlib快速入门
- Apple further entered the financial sector through the 'virtual card' security function in IOS 16
猜你喜欢
ASP.NET Core入门五
Gazebo import the mapping model created by blender
新版代挂网站PHP源码+去除授权/支持燃鹅代抽
ASEMI整流桥KBPC1510的型号数字代表什么
「开源摘星计划」Loki实现Harbor日志的高效管理
行测-图形推理-7-相异图形类
UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xf9 in position 56: illegal multibyte sequence
Ueeditor custom display insert code
Matplotlib快速入门
Redis cluster installation
随机推荐
Remember that a development is encountered in the pit of origin string sorting
Aspose. Word operation word document (I)
This experimental syntax requires enabling the parser plugin: ‘optionalChaining‘
Revit secondary development - operation family documents
Leetcode interview question 02.07 Linked list intersection [double pointer]
数字化转型:五个步骤推动企业进步
Two methods of calling WCF service by C #
Quick sort (diagram +c code)
Leetcode1984. Minimum difference in student scores
Revit secondary development - Hide occlusion elements
Matplotlib快速入门
Record problems fgui tween animation will be inexplicably killed
ASP. Net core introduction V
Revit secondary development - cut view
How to choose the appropriate automated testing tools?
Ni9185 and ni9234 hardware settings in Ni Max
行測-圖形推理-4-字母類
vite Unrestricted file system access to
Kaggle-Titanic
Line test - graphic reasoning - 4 - alphabetic class