当前位置:网站首页>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.
边栏推荐
- How do programmers without objects spend the Chinese Valentine's Day
- 特殊矩阵的压缩存储
- HDU 1114:Piggy-Bank ← 完全背包问题
- View handler 踩坑记录
- 正则表达式,匹配中间的某一段字符串
- The design idea of DMicro, the Go microservice development framework
- 01 [Foreword Basic Use Core Concepts]
- [C language] Detailed explanation of stacks and queues (define, destroy, and data operations)
- QT语言文件制作
- Pisanix v0.2.0 released | Added support for dynamic read-write separation
猜你喜欢

What should I do if the self-incrementing id of online MySQL is exhausted?

Unleashing the engine of technological innovation, Intel joins hands with ecological partners to promote the vigorous development of smart retail

The 20th day of the special assault version of the sword offer

RAID disk array

【解密】OpenSea免费创造的NFT都没上链竟能出现在我的钱包里?

KingbaseES V8 GIS data migration solution (2. Introduction to the capabilities of Kingbase GIS)

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

select tag custom style

特殊矩阵的压缩存储

C language diary 9 3 kinds of statements of if
随机推荐
Gantt chart is here, project management artifact, template is used directly
[LeetCode Brush Questions] - Sum of Numbers topic (more topics to be added)
Compressed storage of special matrices
Hypervisor related knowledge points
the mechanism of ideology
[C language] Detailed explanation of stacks and queues (define, destroy, and data operations)
torch.roll()
Regular expression to match a certain string in the middle
【解密】OpenSea免费创造的NFT都没上链竟能出现在我的钱包里?
Error: Not a signal or slot declaration
【C语言】详解栈和队列(定义、销毁、数据的操作)
2022-08-04:输入:去重数组arr,里面的数只包含0~9。limit,一个数字。 返回:要求比limit小的情况下,能够用arr拼出来的最大数字。 来自字节。
用@Mapper查询oracle的分区情况报错
1873. 计算特殊奖金
Note that Weifang generally needs to pay attention to issuing invoices
Semi-Decentralized Federated Learning for Cooperative D2D Local Model Aggregation
mysql树状结构查询问题
matlab绘制用颜色表示模值大小的箭头图
C language diary 9 3 kinds of statements of if
Simple implementation of YOLOv7 pre-training model deployment based on OpenVINO toolkit