当前位置:网站首页>力扣-104. 二叉树的最大深度
力扣-104. 二叉树的最大深度
2022-06-25 09:32:00 【抗争的小青年】
104. 二叉树的最大深度
难度:简单
思路:定义两个变量,分别用来保存二叉树的最大深度,和当前遍历的深度。然后定义一个traverse函数,用来对二叉树进行遍历。
/** * 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 {
//定义一个变量用来保存最大深度
int maxDtph = 0;
//定义一个用来保存当前遍历到的深度的变量 levelDth
int levelDth = 0;
public int maxDepth(TreeNode root) {
traverse(root);
return maxDtph;
}
//定义一个函数用来遍历二叉树
void traverse(TreeNode root){
//边界值判断
if(root == null){
return ;
}
//深度+1
levelDth++;
//如果当前遍历到深度 大于 用来记录二叉树深度 的值。那么就把这个值赋值给maxDtph
if(levelDth > maxDtph){
maxDtph = levelDth;
}
traverse(root.left);
traverse(root.right);
//后序遍历
levelDth--;
}
}
提示
用到了遍历递归的思维
边栏推荐
- Voiceprint Technology (VII): the future of voiceprint Technology
- matplotlib 简单逻辑回归可视化
- Oracle-单行函数大全
- 51 SCM time stamp correlation function
- 2022 meisai question a idea sharing
- Data-driven anomaly detection and early warning of 21 May Day C
- Is it safe to open an account with Great Wall Securities by mobile phone?
- vscode试图过程写入管道不存在
- Work of the 15th week
- Voiceprint Technology (I): the past and present life of voiceprint Technology
猜你喜欢

2022 meisai question a idea sharing
![[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

compiling stm32f4xx_ it. c... “.\Objects\BH-F407.axf“ - 42 Error(s), 1 Warning(s).
![[project part - structure and content writing of technical scheme] software system type mass entrepreneurship and innovation project plan and Xinmiao guochuang (Dachuang) application](/img/4a/1e83ea9e8e79c0ae7244a159943480.jpg)
[project part - structure and content writing of technical scheme] software system type mass entrepreneurship and innovation project plan and Xinmiao guochuang (Dachuang) application
![[opencv] - input and output XML and yaml files](/img/4e/7944e205c71246d0b0e3747eefca37.png)
[opencv] - input and output XML and yaml files

2021mathorcupc topic optimal design of heat dissipation for submarine data center

2、 Training fashion_ MNIST dataset

Matplotlib simple logistic regression visualization

Online notes on Mathematics for postgraduate entrance examination (8): Kego equations, eigenvalues and eigenvectors, similarity matrix, quadratic series courses

The first techo day Tencent technology open day, 628 waiting for you!
随机推荐
Notes on key words in the original English work biography of jobs (VI) [chapter three]
Is it safe to open an account in a mobile phone or a securities company?
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
【mysql学习笔记20】mysql体系结构
[zero foundation understanding innovation and entrepreneurship competition] overall cognition and introduction of mass entrepreneurship and innovation competition (including FAQs and integration of bl
C语言刷题随记 —— 猴子吃桃
Flutter multilingual intl: ^0.17.0 cannot be imported
How to download the school logo, school name and corporate logo on a transparent background without matting
How to delete a blank page that cannot be deleted in word
sklearn PolynomialFeatures的具体用法
Abbreviations of common English terms for IC R & D
将jar包注册为服务,实现开机自动启动
Work of the 15th week
【mysql学习笔记21】存储引擎
Is it safe to open an account online? Who can I ask?
When unity released webgl, jsonconvert Serializeobject() conversion failed
Solution to the problem of repeated startup of esp8266
Specific usage of sklearn polynomialfeatures
手机办理长城证券开户靠谱安全吗?
纳米数据世界杯数据接口,中超数据,体育数据比分,世界杯赛程api,足球比赛实时数据接口