当前位置:网站首页>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.
边栏推荐
猜你喜欢
Optimizing the feed flow encountered obstacles, who helped Baidu break the "memory wall"?
海量服务实例动态化管理
Intel XDC 2022 Wonderful Review: Build an Open Ecosystem and Unleash the Potential of "Infrastructure"
VSCode Change Default Terminal how to modify the Default Terminal VSCode
leetcode 15
Advanced Numbers_Review_Chapter 1: Functions, Limits, Continuity
nodeJs--封装路由
基于左序遍历的数据存储实践
DAY22: sqli-labs shooting range clearance wp (Less01~~Less20)
Simple implementation of YOLOv7 pre-training model deployment based on OpenVINO toolkit
随机推荐
Apache DolphinScheduler新一代分布式工作流任务调度平台实战-中
用@Mapper查询oracle的分区情况报错
[深入研究4G/5G/6G专题-51]: URLLC-16-《3GPP URLLC相关协议、规范、技术原理深度解读》-11-高可靠性技术-2-链路自适应增强(根据无线链路状态动态选择高可靠性MCS)
OpenGL 工作原理
VSCode Change Default Terminal 如何修改vscode的默认terminal
js中try...catch和finally的用法
重新审视分布式系统:永远不会有完美的一致性方案……
The 20th day of the special assault version of the sword offer
sql语句多字段多个值如何进行排序
[In-depth study of 4G/5G/6G topic-51]: URLLC-16-"3GPP URLLC related protocols, specifications, and technical principles in-depth interpretation"-11-High reliability technology-2-Link adaptive enhancem
torch.roll()
1667. 修复表中的名字
HDU 1114:Piggy-Bank ← 完全背包问题
C语言日记 9 if的3种语句
leetcode-另一棵树的子树
云原生(三十二) | Kubernetes篇之平台存储系统介绍
QT:神奇QVarient
C学生管理系统 指定位置插入学生节点
线上MySQL的自增id用尽怎么办?
627. 变更性别