当前位置:网站首页>Lexicon - the maximum depth of a binary tree
Lexicon - the maximum depth of a binary tree
2022-08-05 02:33:00 【qq_52025208】
Title description: Given a binary tree, find its maximum depth.
The depth of a binary tree is the number of nodes on the longest path from the root node to the farthest leaf node.
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+span>1;}}Note:
return maxDepth(root.left)>maxDepth(root.right)?maxDepth(root.left)+1:maxDepth(root.right)+1;The time complexity of this way of writing is very large, because it recurses the left subtree and the right subtree twice.
边栏推荐
- Note that Weifang generally needs to pay attention to issuing invoices
- Fragment visibility judgment
- DAY22: sqli-labs shooting range clearance wp (Less01~~Less20)
- Opening - Open a new .NET modern application development experience
- OpenGL 工作原理
- 转:查尔斯·汉迪:你是谁,比你做什么更重要
- The 20th day of the special assault version of the sword offer
- Compressed storage of special matrices
- What should I do if the self-incrementing id of online MySQL is exhausted?
- Industry case | insurance companies of the world's top 500 construction standards can be used to drive the business analysis system
猜你喜欢

虚拟内存原理与技术

剑指offer专项突击版第20天

2022了你还不会『低代码』?数据科学也能玩转Low-Code啦!

Hypervisor related knowledge points
![[LeetCode Brush Questions] - Sum of Numbers topic (more topics to be added)](/img/ee/6b52072c841af99488dc0c1141c74c.png)
[LeetCode Brush Questions] - Sum of Numbers topic (more topics to be added)

"Dilili, wait for the lights, wait for the lights", the prompt sound for safe production in the factory

Jincang database KingbaseES V8 GIS data migration solution (3. Data migration based on ArcGIS platform to KES)

【C语言】详解栈和队列(定义、销毁、数据的操作)

【OpenCV 图像处理2】:OpenCV 基础知识

nodeJs--encapsulate routing
随机推荐
QT语言文件制作
iNFTnews | What can NFTs bring to the sports industry and fans?
What should I do if the self-incrementing id of online MySQL is exhausted?
力扣-相同的树
用@Mapper查询oracle的分区情况报错
Images using redis cache Linux master-slave synchronization server hard drive full of moved to the new directory which points to be modified
OpenGL 工作原理
线性表的查找
解决connect: The requested address is not valid in its context
剑指offer专项突击版第20天
select 标签自定义样式
【C语言】详解栈和队列(定义、销毁、数据的操作)
J9数字货币论:web3的创作者经济是什么?
1527. 患某种疾病的患者
基于左序遍历的数据存储实践
Jincang database KingbaseES V8 GIS data migration solution (3. Data migration based on ArcGIS platform to KES)
数据增强Mixup原理与代码解读
LPQ(局部相位量化)学习笔记
Access Characteristics of Constructor under Inheritance Relationship
2022了你还不会『低代码』?数据科学也能玩转Low-Code啦!