当前位置:网站首页>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
边栏推荐
- 行测-图形推理-8-图群类
- PHP records the pitfalls encountered in the complete docking of Tencent cloud live broadcast and im live group chat
- Leetcode1984. Minimum difference in student scores
- 详解全志V853上的ARM A7和RISC-V E907之间的通信方式
- Early childhood education industry of "screwing bar": trillion market, difficult to be a giant
- LeetCode707. Design linked list
- Ni9185 and ni9234 hardware settings in Ni Max
- Nx10.0 installation tutorial
- 微服务远程Debug,Nocalhost + Rainbond微服务开发第二弹
- Revit secondary development - get the project file path
猜你喜欢
Visual design form QT designer design gui single form program
Quick sort (diagram +c code)
Form组件常用校验规则-2(持续更新中~)
PHP method of obtaining image information
. Net automapper use
Signal feature extraction +lstm to realize gear reducer fault diagnosis -matlab code
UWA问答精选
行测-图形推理-3-对称图形类
Remember an experience of using selectmany
Redis official ORM framework is more elegant than redistemplate
随机推荐
Time convolution Network + soft threshold + attention mechanism to realize residual life prediction of mechanical equipment
Qt Graphicsview图形视图使用总结附流程图开发案例雏形
Build an "immune" barrier in the cloud to prepare your data
How to judge whether the input content is "number"
Get the exact offset of the element
Leetcode interview question 02.07 Linked list intersection [double pointer]
Redis official ORM framework is more elegant than redistemplate
Robot autonomous exploration DSVP: code parsing
Explain in detail the communication mode between arm A7 and risc-v e907 on Quanzhi v853
Understand the autograd package in pytorch
Visual studio 2019 installation
php 记录完整对接腾讯云直播以及im直播群聊 所遇到的坑
Unity technical notes (II) basic functions of scriptableobject
Signal feature extraction +lstm to realize gear reducer fault diagnosis -matlab code
Variables and constants
Force deduction - question 561 - array splitting I - step by step parsing
苹果在iOS 16中通过'虚拟卡'安全功能进一步进军金融领域
Failed to initialize rosdep after installing ROS
Debezium series: source code reading snapshot reader
Remember aximp once Use of exe tool