当前位置:网站首页>Force buckle -104 Maximum depth of binary tree
Force buckle -104 Maximum depth of binary tree
2022-06-25 09:42:00 【Struggling young man】
104. The maximum depth of a binary tree
difficulty : Simple
Ideas : Define two variables , Respectively used to save the maximum depth of the binary tree , And the depth of the current traversal . Then define a traverse function , Used to traverse the binary tree .
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */
class Solution {
// Define a variable to hold the maximum depth
int maxDtph = 0;
// Define a variable to hold the current traversal depth levelDth
int levelDth = 0;
public int maxDepth(TreeNode root) {
traverse(root);
return maxDtph;
}
// Define a function to traverse a binary tree
void traverse(TreeNode root){
// Boundary value judgment
if(root == null){
return ;
}
// depth +1
levelDth++;
// If the current traversal reaches the depth Greater than Used to record the depth of binary tree Value . Then assign this value to maxDtph
if(levelDth > maxDtph){
maxDtph = levelDth;
}
traverse(root.left);
traverse(root.right);
// After the sequence traversal
levelDth--;
}
}
Tips
The idea of traversal recursion is used
边栏推荐
- 【mysql学习笔记22】索引
- [learn C from me and master the key to programming] insertion sort of eight sorts
- x86电脑上下载debian的arm64的包
- Voiceprint Technology (V): voiceprint segmentation and clustering technology
- Is it safe to open an account with Great Wall Securities by mobile phone?
- PMP考试多少分算通过?
- Study on correlation of pumpkin price and design of price prediction model based on BP neural network
- Cassava tree disease recognition based on vgg16 image classification
- 请问在手机上开户股票,还是去证券公司开户安全?
- Matplotlib simple logistic regression visualization
猜你喜欢

Reasons for Meiye to choose membership system

SQL advanced
![[design completion - opening report] zufeinfo 2018 software engineering major (including FAQ)](/img/b7/fb7ead787e00047f3663e8b5acfe33.jpg)
[design completion - opening report] zufeinfo 2018 software engineering major (including FAQ)

Rxjs TakeUntil 操作符的学习笔记

Lvs-dr mode single network segment case

纳米数据世界杯数据接口,中超数据,体育数据比分,世界杯赛程api,足球比赛实时数据接口

Data-driven anomaly detection and early warning of item C in the May 1st mathematical modeling competition in 2021

Compile time annotations for custom annotations (retentionpolicy.class)

Wechat official account can reply messages normally, but it still prompts that the service provided by the official account has failed. Please try again later
![[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate](/img/75/a06e20b4394579cbd9f6d3a075907a.jpg)
[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate
随机推荐
Online notes on Mathematics for postgraduate entrance examination (8): Kego equations, eigenvalues and eigenvectors, similarity matrix, quadratic series courses
js工具函数,自己封装一个节流函数
[Ruby on rails full stack course] course directory
Compare and explain common i/o models
【mysql学习笔记21】存储引擎
Rxjs TakeUntil 操作符的学习笔记
[buuctf.reverse] 121-125
Format analysis and explanation of wav file
MySQL创建给出语句
Japanese online notes for postgraduate entrance examination (9): composition template
C语言刷题随记 —— 猴子吃桃
2台三菱PLC走BCNetTCP协议,能否实现网口无线通讯?
《JVM》对象内存分配的TLAB机制与G1中的TLAB流程
Reasons for Meiye to choose membership system
How to download the school logo, school name and corporate logo on a transparent background without matting
8. Intelligent transportation project (1)
Voiceprint Technology (II): Fundamentals of audio signal processing
[MySQL learning notes 22] index
[buuctf.reverse] 121-125
Online notes on Mathematics for postgraduate entrance examination (9): a series of courses on probability theory and mathematical statistics