当前位置:网站首页>【 剑指 Offer】55 - I. 二叉树的深度
【 剑指 Offer】55 - I. 二叉树的深度
2022-07-01 13:26:00 【LuZhouShiLi】
剑指 Offer 55 - I. 二叉树的深度
题目
输入一棵二叉树的根节点,求该树的深度。从根节点到叶节点依次经过的节点(含根、叶节点)形成树的一条路径,最长路径的长度为树的深度。
思路
节点既有左子树又有右子树,那么树的深度就是左右子树深度的较大值再加1。
代码
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */
class Solution {
public:
int maxDepth(TreeNode* root) {
if(root == nullptr)
{
return 0;
}
int left = maxDepth(root->left);
int right = maxDepth(root->right);
return (left > right) ? (left + 1) :(right + 1);
}
};
边栏推荐
- MySQL 66 questions, 20000 words + 50 pictures in detail! Necessary for review
- Flutter SQLite使用
- 焱融看 | 混合云时代下,如何制定多云策略
- A Fletter version of Notepad
- How much money do novices prepare to play futures? Is agricultural products OK?
- Build a vc2010 development environment and create a tutorial of "realizing Tetris game in C language"
- Machine learning summary (I): linear regression, ridge regression, Lasso regression
- 5. Use of ly tab plug-in of header component
- Report on the 14th five year plan and future development trend of China's integrated circuit packaging industry Ⓓ 2022 ~ 2028
- Declare an abstract class vehicle, which contains the private variable numofwheel and the public functions vehicle (int), horn (), setnumofwheel (int) and getnumofwheel (). Subclass mot
猜你喜欢

Beidou communication module Beidou GPS module Beidou communication terminal DTU

AnimeSR:可学习的降质算子与新的真实世界动漫VSR数据集

详细讲解面试的 IO多路复用,select,poll,epoll
Example code of second kill based on MySQL optimistic lock

Content Audit Technology
![[development of large e-commerce projects] performance pressure test - basic concept of pressure test & jmeter-38](/img/50/819b9c2f69534afc6dc391c9de5f05.png)
[development of large e-commerce projects] performance pressure test - basic concept of pressure test & jmeter-38

当你真的学会DataBinding后,你会发现“这玩意真香”!

Computer network interview knowledge points

SAP 智能机器人流程自动化(iRPA)解决方案分享

Understand the window query function of tdengine in one article
随机推荐
Yarn重启applications记录恢复
Arthas use
5G工业网关的科技治超应用 超限超重超速非现场联合执法
Google Earth engine (GEE) - Global Human Settlements grid data 1975-1990-2000-2014 (p2016)
Flutter SQLite使用
陈宇(Aqua)-安全->云安全->多云安全
单工,半双工,全双工区别以及TDD和FDD区别
3.4 《数据库系统概论》之数据查询—SELECT(单表查询、连接查询、嵌套查询、集合查询、多表查询)
[development of large e-commerce projects] performance pressure test - basic concept of pressure test & jmeter-38
北斗通信模块 北斗gps模块 北斗通信终端DTU
Introduction to topological sorting
What is the future development direction of people with ordinary education, appearance and family background? The career planning after 00 has been made clear
Collation and review of knowledge points of Microcomputer Principle and interface technology - pure manual
Content Audit Technology
spark源码阅读总纲
Apache-Atlas-2.2.0 独立编译部署
8 popular recommended style layout
Simple two ball loading
04-Redis源码数据结构之字典
A Fletter version of Notepad