当前位置:网站首页>[sword finger offer] 55 - I. depth of binary tree
[sword finger offer] 55 - I. depth of binary tree
2022-07-01 13:39:00 【LuZhouShiLi】
The finger of the sword Offer 55 - I. The depth of the binary tree
subject
Enter the root node of a binary tree , Find the depth of the tree . The nodes that pass from the root node to the leaf node ( Containing root 、 Leaf nodes ) A path to a tree , The length of the longest path is the depth of the tree .
Ideas
Nodes have both left and right subtrees , Then the depth of the tree is the larger value of the depth of the left and right subtrees plus 1.
Code
/** * 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);
}
};
边栏推荐
- Who should I know when opening a stock account? Is it actually safe to open an account online?
- minimum spanning tree
- 7. Icons
- 微机原理与接口技术知识点整理复习–纯手打
- Summary of 20 practical typescript single line codes
- Self cultivation of open source programmers who contributed tens of millions of lines of code to shardingsphere and later became CEO
- Analysis report on the development trend and prospect scale of silicon intermediary industry in the world and China Ⓩ 2022 ~ 2027
- 北斗通信模块 北斗gps模块 北斗通信终端DTU
- 洞态在某互联⽹⾦融科技企业的最佳落地实践
- 一款Flutter版的记事本
猜你喜欢

啟動solr報錯The stack size specified is too small,Specify at least 328k

龙蜥社区开源 coolbpf,BPF 程序开发效率提升百倍

2022 · 让我带你Jetpack架构组件从入门到精通 — Lifecycle

Google Earth engine (GEE) - Global Human Settlements grid data 1975-1990-2000-2014 (p2016)

1553B环境搭建

Flow management technology
Example code of second kill based on MySQL optimistic lock

【241. 为运算表达式设计优先级】
![[machine learning] VAE variational self encoder learning notes](/img/38/3eb8d9078b2dcbe780430abb15edcb.png)
[machine learning] VAE variational self encoder learning notes

Self cultivation of open source programmers who contributed tens of millions of lines of code to shardingsphere and later became CEO
随机推荐
About fossage 2.0 "meta force meta universe system development logic scheme (details)
Report on the 14th five year plan and future development trend of China's integrated circuit packaging industry Ⓓ 2022 ~ 2028
Shangtang technology crash: a script written at the time of IPO
French Data Protection Agency: using Google Analytics or violating gdpr
5G工业网关的科技治超应用 超限超重超速非现场联合执法
关于佛萨奇2.0“Meta Force原力元宇宙系统开发逻辑方案(详情)
6年技术迭代,阿里全球化出海&合规的挑战和探索
Analysis report on the development trend and prospect scale of silicon intermediary industry in the world and China Ⓩ 2022 ~ 2027
Cs5268 advantages replace ag9321mcq typec multi in one docking station scheme
20个实用的 TypeScript 单行代码汇总
Enter the top six! Boyun's sales ranking in China's cloud management software market continues to rise
Understand the window query function of tdengine in one article
Analysis report on the development prospect and investment strategic planning of China's wafer manufacturing Ⓔ 2022 ~ 2028
IO的几种模型 阻塞,非阻塞,io多路复用,信号驱动和异步io
当你真的学会DataBinding后,你会发现“这玩意真香”!
SAP intelligent robot process automation (IRPA) solution sharing
Collation and review of knowledge points of Microcomputer Principle and interface technology - pure manual
Analysis report on the development prospect and investment strategy of the global and Chinese laser chip industry Ⓑ 2022 ~ 2027
1553B环境搭建
How much money do novices prepare to play futures? Is agricultural products OK?