当前位置:网站首页>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.
边栏推荐
猜你喜欢

Matlab画图3

SuperMap iDesktop.Net之布尔运算求交——修复含拓扑错误复杂模型

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

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

Simple implementation of YOLOv7 pre-training model deployment based on OpenVINO toolkit

Using OpenVINO to implement the flying paddle version of the PGNet inference program

DAY22: sqli-labs shooting range clearance wp (Less01~~Less20)

关于#sql shell#的问题,如何解决?
![[ROS] (10) ROS Communication - Service Communication](/img/4d/4657f24bd7809abb4bdc4b418076f7.png)
[ROS] (10) ROS Communication - Service Communication

C语言日记 9 if的3种语句
随机推荐
KingbaseES V8 GIS data migration solution (2. Introduction to the capabilities of Kingbase GIS)
Unleashing the engine of technological innovation, Intel joins hands with ecological partners to promote the vigorous development of smart retail
Programmer's list of sheep counting when insomnia | Daily anecdote
C学生管理系统 据学号查找学生节点
Semi-Decentralized Federated Learning for Cooperative D2D Local Model Aggregation
Using OpenVINO to implement the flying paddle version of the PGNet inference program
Industry case | insurance companies of the world's top 500 construction standards can be used to drive the business analysis system
SDC简介
Access Characteristics of Constructor under Inheritance Relationship
Regular expression to match a certain string in the middle
The 20th day of the special assault version of the sword offer
C student management system Insert the student node at the specified location
the mechanism of ideology
继承关系下构造方法的访问特点
C language implements a simple number guessing game
How do programmers without objects spend the Chinese Valentine's Day
post-study program
02 【开发服务器 资源模块】
1484. 按日期分组销售产品
View handler 踩坑记录