当前位置:网站首页>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.
边栏推荐
- J9数字货币论:web3的创作者经济是什么?
- Jincang database KingbaseES V8 GIS data migration solution (3. Data migration based on ArcGIS platform to KES)
- 关于#sql shell#的问题,如何解决?
- Access Characteristics of Constructor under Inheritance Relationship
- 力扣-相同的树
- select 标签自定义样式
- 【存储】曙光存储DS800-G35 ISCSI各映射LUN给服务器
- Semi-Decentralized Federated Learning for Cooperative D2D Local Model Aggregation
- RAID磁盘阵列
- C language implements a simple number guessing game
猜你喜欢
![01 [Foreword Basic Use Core Concepts]](/img/90/67537d5fad28d68766ca85b887839e.png)
01 [Foreword Basic Use Core Concepts]

HOG feature study notes

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

js中try...catch和finally的用法

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

学习笔记-----左偏树

Matlab map with color representation module value size arrow

多线程(2)

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

Unleashing the engine of technological innovation, Intel joins hands with ecological partners to promote the vigorous development of smart retail
随机推荐
post-study program
Matlab画图3
LPQ(局部相位量化)学习笔记
回顾51单片机
shell语句修改txt文件或者sh文件
VSCode Change Default Terminal 如何修改vscode的默认terminal
C language implements a simple number guessing game
Optimizing the feed flow encountered obstacles, who helped Baidu break the "memory wall"?
云原生(三十二) | Kubernetes篇之平台存储系统介绍
627. 变更性别
select 标签自定义样式
2022-08-04:输入:去重数组arr,里面的数只包含0~9。limit,一个数字。 返回:要求比limit小的情况下,能够用arr拼出来的最大数字。 来自字节。
C学生管理系统 头添加学生节点
C语言日记 9 if的3种语句
Images using redis cache Linux master-slave synchronization server hard drive full of moved to the new directory which points to be modified
C student management system Find student nodes based on student ID
【LeetCode刷题】-数之和专题(待补充更多题目)
继承关系下构造方法的访问特点
js中try...catch和finally的用法
How to deal with your own shame