当前位置:网站首页>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
边栏推荐
- Revit secondary development - wall opening
- Redis官方ORM框架比RedisTemplate更优雅
- Debezium series: introducing support for the final operator
- Class implementation of linear stack and linear queue (another binary tree pointer version)
- Record problems fgui tween animation will be inexplicably killed
- Debezium系列之:源码阅读之BinlogReader
- Yarn开启ACL用户认证之后无法查看Yarn历史任务日志解决办法
- [environment] pycharm sets the tool to convert QRC into py file
- JS number is insufficient, and 0 is added
- Write in front -- Talking about program development
猜你喜欢

How to choose the appropriate automated testing tools?

0-5VAC转4-20mA交流电流隔离变送器/转换模块

Yarn cannot view the historical task log of yarn after enabling ACL user authentication. Solution

Robot autonomous exploration DSVP: code parsing

How to judge whether the input content is "number"
![LeetCode206. Reverse linked list [double pointer and recursion]](/img/3c/84351e771ac9763c1e5f7b4921c099.jpg)
LeetCode206. Reverse linked list [double pointer and recursion]

How to choose the appropriate automated testing tools?
![VTOL in Px4_ att_ Control source code analysis [supplement]](/img/7a/4ce0c939b9259faf59c52da2587693.jpg)
VTOL in Px4_ att_ Control source code analysis [supplement]

Line test - graphic reasoning - 3 - symmetric graphic class

Ni9185 and ni9234 hardware settings in Ni Max
随机推荐
Antd date component appears in English
How to choose the appropriate automated testing tools?
Quick sort (diagram +c code)
关于海康ipc的几个参数
Matplotlib快速入门
Ligne - raisonnement graphique - 4 - classe de lettres
Revit secondary development - operation family documents
Line test - graphic reasoning -5- one stroke class
Welcome to CSDN markdown editor
Ueeditor custom display insert code
LeetCode203. Remove linked list elements
LeetCode144. Preorder traversal of binary tree
Redis官方ORM框架比RedisTemplate更优雅
新版代挂网站PHP源码+去除授权/支持燃鹅代抽
Revit secondary development - shielding warning prompt window
Debezium系列之:mysql墓碑事件
软件测评中心▏自动化测试有哪些基本流程和注意事项?
CTF练习
“拧巴”的早教行业:万亿市场,难出巨头
Attitude estimation (complementary filtering)