当前位置:网站首页>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
边栏推荐
- What does it mean to prefix a string with F?
- “拧巴”的早教行业:万亿市场,难出巨头
- VTOL in Px4_ att_ Control source code analysis [supplement]
- Write in front -- Talking about program development
- Time convolution Network + soft threshold + attention mechanism to realize residual life prediction of mechanical equipment
- Matplotlib quick start
- How to close eslint related rules
- 7-51 combination of two ordered linked list sequences
- . Net automapper use
- Remember that a development is encountered in the pit of origin string sorting
猜你喜欢
Leetcode206. Reverse linked list
How to choose the appropriate automated testing tools?
Early childhood education industry of "screwing bar": trillion market, difficult to be a giant
Vs custom template - take the custom class template as an example
Basic knowledge of binary tree
Redis official ORM framework is more elegant than redistemplate
Signal feature extraction +lstm to realize gear reducer fault diagnosis -matlab code
数字化转型:五个步骤推动企业进步
Basic knowledge of linked list
ASEMI整流桥KBPC1510的型号数字代表什么
随机推荐
数字化转型:五个步骤推动企业进步
Redis集群安装
Matplotlib快速入门
What does it mean to prefix a string with F?
0-5VAC转4-20mA交流电流隔离变送器/转换模块
行测-图形推理-5-一笔画类
Revit secondary development - cut view
Select sort (illustration +c code)
Debezium series: source code reading snapshot reader
Xcode modifies the default background image of launchscreen and still displays the original image
Explain in detail the communication mode between arm A7 and risc-v e907 on Quanzhi v853
Line test - graphic reasoning - 2 - black and white lattice class
Common verification rules of form components -2 (continuously updating ~)
行测-图形推理-9-线条问题类
De la famille debezium: SET ROLE statements supportant mysql8
Form组件常用校验规则-2(持续更新中~)
微服务远程Debug,Nocalhost + Rainbond微服务开发第二弹
变量与常量
Variables and constants
How to close eslint related rules