当前位置:网站首页>力扣-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--;
}
}
提示
用到了遍历递归的思维
边栏推荐
- How to download the school logo, school name and corporate logo on a transparent background without matting
- Analysis on the thinking of 2022 meisai C question
- PMP考试多少分算通过?
- Socket programming -- epoll model
- sklearn 高维数据集制作make_circles 和 make_moons
- What functions should smart agriculture applet system design have
- Atguigu---18-component
- Use Navicat to compare data differences and structure differences of multi environment databases, and automatic DML and DDL scripts
- Compile time annotations for custom annotations (retentionpolicy.class)
- [buuctf.reverse] 117-120
猜你喜欢

Numpy numpy中的meshgrid()函数

The first techo day Tencent technology open day, 628 waiting for you!

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

自定义注解之编译时注解(RetentionPolicy.CLASS)

Data-driven anomaly detection and early warning of 21 May Day C

x86的编码格式

203 postgraduate entrance examination Japanese self-study postgraduate entrance examination experience post; Can I learn Japanese by myself?
![[buuctf.reverse] 117-120](/img/6c/8a90fff2bd46f1494a9bd9c77eeafc.png)
[buuctf.reverse] 117-120

vscode试图过程写入管道不存在

【mysql学习笔记22】索引
随机推荐
Compile time annotations for custom annotations (retentionpolicy.class)
Where is safe for FTSE A50 to open an account
Where are the hotel enterprises that have been under pressure since the industry has warmed up in spring?
Summarize two methods of configuring pytorch GPU environment
备战2022年金九银十必问的1000道Android面试题及答案整理,彻底解决面试的烦恼
Notes on key words in the original English work biography of jobs (V) [chapter three]
Voiceprint Technology (I): the past and present life of voiceprint Technology
On the underlying index principle of MySQL
[matlab] image binarization (imbinarize function)
3、 Automatically terminate training
2022 postgraduate entrance examination experience post -- Alibaba Business School of Hangzhou Normal University -- management science and Engineering (including the recommendation of books and course
Numpy numpy中的meshgrid()函数
云网络技术的好处以及类型
Voiceprint Technology (III): voiceprint recognition technology
Is it safe to open an account with Great Wall Securities by mobile phone?
证券开户风险大吗,安全靠谱吗?
Nodejs using the express framework demo
[learn C from me and master the key to programming] insertion sort of eight sorts
Prepare for the 1000 Android interview questions and answers that golden nine silver ten must ask in 2022, and completely solve the interview problems
将jar包注册为服务,实现开机自动启动