当前位置:网站首页>力扣-二叉树的最大的深度
力扣-二叉树的最大的深度
2022-08-05 02:00:00 【qq_52025208】
题目描述:给定一个二叉树,找出其最大深度。
二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
class Solution {
public int maxDepth(TreeNode root) {
if(root == null) return 0;
int leftHeight = maxDepth(root.left);
int rightHeight = maxDepth(root.right);
return leftHeight>rightHeight?leftHeight+1:rightHeight+1;
}
}
注意:
return maxDepth(root.left)>maxDepth(root.right)?maxDepth(root.left)+1:maxDepth(root.right)+1;
这种写法时间复杂度会非常大,因为它两次递归左子树和右子树。
边栏推荐
猜你喜欢

How do programmers without objects spend the Chinese Valentine's Day

软件测试技术之最有效的七大性能测试技术

刷爆朋友圈,Alibaba出品亿级并发设计速成笔记太香了

Day Fourteen & Postman

iNFTnews | What can NFTs bring to the sports industry and fans?

关于#sql shell#的问题,如何解决?

如何逐步执行数据风险评估

【Unity入门计划】2D游戏中遮挡问题的处理方法&伪透视

Three handshake and four wave in tcp

如何发现一个有价值的 GameFi?
随机推荐
Are testing jobs so hard to find?I am 32 this year and I have been unemployed for 2 months. What should an older test engineer do next to support his family?
Leetcode brushing questions - 22. Bracket generation
JZ搜索引擎solr研究-从数据库创建索引
"Dilili, wait for the lights, wait for the lights", the prompt sound for safe production in the factory
pytorch的使用:卷积神经网络模块
[Machine Learning] 21-day Challenge Study Notes (2)
(十七)51单片机——AD/DA转换
【genius_platform软件平台开发】第七十六讲:vs预处理器定义的牛逼写法!!!!(其他组牛逼conding人员告知这么配置来取消宏定义)
软件测试技术之最有效的七大性能测试技术
C language basics -- pointers
".NET IoT from scratch" series
A new technical director, who calls DDD a senior, is convinced
Methods commonly used interface automation test framework postman tests
PHP技能评测
.Net C# Console Create a window using Win32 API
CPDA|运营人如何从负基础学会数据分析(SQL)
【机器学习】21天挑战赛学习笔记(二)
The use of pytorch: temperature prediction using neural networks
如何发现一个有价值的 GameFi?
缺陷检测(图像处理部分)