当前位置:网站首页>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.
边栏推荐
- 【LeetCode刷题】-数之和专题(待补充更多题目)
- Intel XDC 2022 Wonderful Review: Build an Open Ecosystem and Unleash the Potential of "Infrastructure"
- LPQ (local phase quantization) study notes
- 力扣-二叉树的最大的深度
- 1873. 计算特殊奖金
- leetcode-对称二叉树
- QT语言文件制作
- Simple implementation of YOLOv7 pre-training model deployment based on OpenVINO toolkit
- 【解密】OpenSea免费创造的NFT都没上链竟能出现在我的钱包里?
- C language implements a simple number guessing game
猜你喜欢
OpenGL 工作原理
【C语言】详解栈和队列(定义、销毁、数据的操作)
Simple implementation of YOLOv7 pre-training model deployment based on OpenVINO toolkit
02 【开发服务器 资源模块】
海量服务实例动态化管理
Programmer's list of sheep counting when insomnia | Daily anecdote
Go 微服务开发框架 DMicro 的设计思路
"Dilili, wait for the lights, wait for the lights", the prompt sound for safe production in the factory
RAID磁盘阵列
Matlab map with color representation module value size arrow
随机推荐
HOG feature study notes
Semi-Decentralized Federated Learning for Cooperative D2D Local Model Aggregation
云原生(三十二) | Kubernetes篇之平台存储系统介绍
使用SuperMap iDesktopX数据迁移工具迁移ArcGIS数据
The design idea of DMicro, the Go microservice development framework
Dotnet 6 Why does the network request not follow the change of the system network proxy and dynamically switch the proxy?
学习笔记-----左偏树
Intel XDC 2022 Wonderful Review: Build an Open Ecosystem and Unleash the Potential of "Infrastructure"
行业案例|世界 500 强险企如何建设指标驱动的经营分析系统
Domain Driven Design - MDD
1527. 患某种疾病的患者
DAY22: sqli-labs shooting range clearance wp (Less01~~Less20)
iNFTnews | What can NFTs bring to the sports industry and fans?
Programmer's list of sheep counting when insomnia | Daily anecdote
线性表的查找
QT:神奇QVarient
nodeJs--封装路由
select tag custom style
Tree search (bintree)
mysql tree structure query problem