当前位置:网站首页>力扣-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--;
}
}
提示
用到了遍历递归的思维
边栏推荐
- 自定义注解之编译时注解(RetentionPolicy.CLASS)
- Creating a binary tree (binary linked list) from a generalized table
- Applet cloud development joint table data query and application in cloud function
- PMP考试多少分算通过?
- 【OpenCV】—输入输出XML和YAML文件
- Notes on key vocabulary of the original English work biography of jobs (I) [introduction]
- [competition - Rural Revitalization] experience sharing of Zhejiang Rural Revitalization creative competition
- The first techo day Tencent technology open day, 628 waiting for you!
- Fcpx quickly add subtitles | Final Cut Pro import fcpxml subtitle file does not match the video time? I got it in code
- When unity released webgl, jsonconvert Serializeobject() conversion failed
猜你喜欢
![[matlab] image binarization (imbinarize function)](/img/3e/066f460d9f436bbc43ea35e46093e2.jpg)
[matlab] image binarization (imbinarize function)

Voiceprint Technology (VI): other applications of voiceprint Technology

Matplotlib decision boundary drawing function plot in Matplotlib_ decision_ Boundary and plt Detailed explanation of contour function

C语言刷题随记 —— 猴子吃桃

C # startup program loses double quotation marks for parameters passed. How to solve it?

8、智慧交通项目(1)

Matplotlib simple logistic regression visualization

Matplotlib plt Axis() usage

When unity released webgl, jsonconvert Serializeobject() conversion failed
![[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
随机推荐
Jmeter接口测试,关联接口实现步骤(token)
Analysis on the bottom calling process of micro service calling component ribbon
matplotlib matplotlib中决策边界绘制函数plot_decision_boundary和plt.contourf函数详解
compiling stm32f4xx_ it. c... “.\Objects\BH-F407.axf“ - 42 Error(s), 1 Warning(s).
What functions should smart agriculture applet system design have
【OpenCV】—离散傅里叶变换
Is the client that gets the scanning code wechat or Alipay
Matplotlib axvline() and axhline() functions in Matplotlib
How much money have I made by sticking to fixed investment for 3 years?
Data-driven anomaly detection and early warning of 21 May Day C
vscode试图过程写入管道不存在
Compile time annotations for custom annotations (retentionpolicy.class)
matplotlib matplotlib中plt.axis()用法
[IOU] intersection over union
Are the top ten securities companies at great risk of opening accounts and safe and reliable?
Data-driven anomaly detection and early warning of item C in the May 1st mathematical modeling competition in 2021
Is it harder to find a job in 2020? Do a good job in these four aspects and find a good job with high salary
jmeter中csv参数化
Notes on key words in the original English work biography of jobs (VI) [chapter three]
【mysql学习笔记20】mysql体系结构