当前位置:网站首页>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.
边栏推荐
- HOG feature study notes
- Access Characteristics of Constructor under Inheritance Relationship
- 力扣-二叉树的前序遍历、中序遍历、后序遍历
- the mechanism of ideology
- 1527. 患某种疾病的患者
- Industry case | insurance companies of the world's top 500 construction standards can be used to drive the business analysis system
- lua learning
- C学生管理系统 头添加学生节点
- Error: Not a signal or slot declaration
- 力扣-相同的树
猜你喜欢
js中try...catch和finally的用法
QT语言文件制作
How do programmers without objects spend the Chinese Valentine's Day
LPQ(局部相位量化)学习笔记
HOG特征学习笔记
剑指offer专项突击版第20天
Pisanix v0.2.0 发布|新增动态读写分离支持
Go 微服务开发框架 DMicro 的设计思路
What should I do if the self-incrementing id of online MySQL is exhausted?
[LeetCode Brush Questions] - Sum of Numbers topic (more topics to be added)
随机推荐
1667. 修复表中的名字
Live preview | 30 minutes started quickly!Look at credible distributed AI chain oar architectural design
[Decryption] Can the NFTs created by OpenSea for free appear in my wallet without being chained?
torch.roll()
Matlab画图3
关于#sql shell#的问题,如何解决?
C student management system head to add a student node
C语言日记 9 if的3种语句
leetcode-对称二叉树
C language diary 9 3 kinds of statements of if
Solve connect: The requested address is not valid in its context
【MySQL series】- Does LIKE query start with % will make the index invalid?
【存储】曙光存储DS800-G35 ISCSI各映射LUN给服务器
Tree search (bintree)
【日常训练】1403. 非递增顺序的最小子序列
散列表的查找(哈希表)
lua学习
The 2022 EdgeX China Challenge will be grandly opened on August 3
Intel XDC 2022 Wonderful Review: Build an Open Ecosystem and Unleash the Potential of "Infrastructure"
Amazon Cloud Technology joins hands with Thundersoft to build an AIoT platform for industry customers