当前位置:网站首页>力扣-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--;
}
}
提示
用到了遍历递归的思维
边栏推荐
- [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate
- 《JVM》对象内存分配的TLAB机制与G1中的TLAB流程
- Matplotlib axvline() and axhline() functions in Matplotlib
- Prediction of pumpkin price based on BP neural network
- Solution to the problem of repeated startup of esp8266
- 使用Navicat对比多环境数据库数据差异和结构差异,以及自动DML和DDL脚本
- Cassava tree disease recognition based on vgg16 image classification
- 《乔布斯传》英文原著重点词汇笔记(三)【 chapter one】
- Numpy numpy中的meshgrid()函数
- Make a skylearn high-dimensional dataset_ Circles and make_ moons
猜你喜欢

Summarize two methods of configuring pytorch GPU environment

How to download the school logo, school name and corporate logo on a transparent background without matting

SQL advanced
![[buuctf.reverse] 117-120](/img/6c/8a90fff2bd46f1494a9bd9c77eeafc.png)
[buuctf.reverse] 117-120

3 big questions! Redis cache exceptions and handling scheme summary

JMeter interface test, associated interface implementation steps (token)

【OpenCV】—离散傅里叶变换

The meshgrid() function in numpy

compiling stm32f4xx_ it. c... “.\Objects\BH-F407.axf“ - 42 Error(s), 1 Warning(s).

C语言刷题随记 —— 猴子吃桃
随机推荐
TLAB mechanism of JVM object memory allocation and TLAB process in G1
Benefits and types of cloud network technology
socket编程——poll模型
C语言刷题随记 —— 猴子吃桃
[zero foundation understanding innovation and entrepreneurship competition] overall cognition and introduction of mass entrepreneurship and innovation competition (including FAQs and integration of bl
通过客户经理的开户二维码开股票账户安全吗?
[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate
CSV parameterization in JMeter
Notes on key words in the original English work biography of jobs (II) [chapter one]
C program termination problem clr20r3 solution
Voiceprint Technology (III): voiceprint recognition technology
4、 Convolution neural networks
行业春寒回暖,持续承压的酒店企业于何处破局?
请问在手机上开户股票,还是去证券公司开户安全?
使用Navicat对比多环境数据库数据差异和结构差异,以及自动DML和DDL脚本
Flitter gets the height of the top status bar
3、 Automatically terminate training
1、 Construction of single neural network
Work of the 15th week
Voiceprint Technology (I): the past and present life of voiceprint Technology