当前位置:网站首页>[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);
}
};
边栏推荐
- Some summary of pyqt5 learning (overview of the general meaning of some signals and methods)
- Analysis report on the development prospect and investment strategy of the global and Chinese laser chip industry Ⓑ 2022 ~ 2027
- 进入前六!博云在中国云管理软件市场销量排行持续上升
- Wave animation color five pointed star loader loading JS special effects
- 20个实用的 TypeScript 单行代码汇总
- Global and Chinese silicone defoamer production and marketing demand and investment forecast analysis report Ⓨ 2022 ~ 2027
- B站被骂上了热搜。。
- uni-app实现广告滚动条
- The stack size specified is too small, specify at least 328k
- 2022上半年英特尔有哪些“硬核创新”?看这张图就知道了!
猜你喜欢

Dragon lizard community open source coolbpf, BPF program development efficiency increased 100 times
基于mysql乐观锁实现秒杀的示例代码

Jenkins+webhooks- multi branch parametric construction-

【机器学习】VAE变分自编码器学习笔记

【241. 为运算表达式设计优先级】

Kongsong (Xintong Institute) - cloud security capacity building and trend in the digital era

Spark source code (V) how does dagscheduler taskscheduler cooperate with submitting tasks, and what is the corresponding relationship between application, job, stage, taskset, and task?

流量管理技术

Terminal identification technology and management technology

Application of 5g industrial gateway in scientific and technological overload control; off-site joint law enforcement for over limit, overweight and overspeed
随机推荐
Several models of IO blocking, non blocking, IO multiplexing, signal driven and asynchronous IO
Blind box NFT digital collection platform system development (build source code)
Grafana reports an error: error= "failed to send notification to email addresses: [email protected] : 535 Error:
04 redis source code data structure dictionary
Colorful five pointed star SVG dynamic web page background JS special effect
Report on the current situation and development trend of bidirectional polypropylene composite film industry in the world and China Ⓟ 2022 ~ 2028
String input function
LeetCode重建二叉树详解[通俗易懂]
word2vec训练中文词向量
Arthas use
Apache-Atlas-2.2.0 独立编译部署
MySQL六十六问,两万字+五十图详解!复习必备
2022 · 让我带你Jetpack架构组件从入门到精通 — Lifecycle
Example code of second kill based on MySQL optimistic lock
La taille de la pile spécifiée est petite, spécifiée à la sortie 328k
分布式事务简介(seata)
Simplex, half duplex, full duplex, TDD and FDD
Judea pearl, Turing prize winner: 19 causal inference papers worth reading recently
leetcode 322. Coin change (medium)
Sharing with the best paper winner of CV Summit: how is a good paper refined?