当前位置:网站首页>[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);
}
};
边栏推荐
- Terminal identification technology and management technology
- 流量管理技术
- Flow management technology
- minimum spanning tree
- 2022上半年英特尔有哪些“硬核创新”?看这张图就知道了!
- Google Earth engine (GEE) - Global Human Settlements grid data 1975-1990-2000-2014 (p2016)
- Understand the window query function of tdengine in one article
- Kongsong (Xintong Institute) - cloud security capacity building and trend in the digital era
- JS discolored Lego building blocks
- B站被骂上了热搜。。
猜你喜欢
[241. Design priority for operation expression]
A Fletter version of Notepad
04 redis source code data structure dictionary
1553B环境搭建
进入前六!博云在中国云管理软件市场销量排行持续上升
Wave animation color five pointed star loader loading JS special effects
终端识别技术和管理技术
2022上半年英特尔有哪些“硬核创新”?看这张图就知道了!
学历、长相、家境普通的人,未来的发展方向是什么?00后的职业规划都已经整得明明白白......
Professor Li Zexiang, Hong Kong University of science and technology: I'm wrong. Why is engineering consciousness more important than the best university?
随机推荐
分布式事务简介(seata)
刘对(火线安全)-多云环境的风险发现
Report on the 14th five year plan and future development trend of China's integrated circuit packaging industry Ⓓ 2022 ~ 2028
arthas使用
Report on the current situation and development trend of bidirectional polypropylene composite film industry in the world and China Ⓟ 2022 ~ 2028
Example code of second kill based on MySQL optimistic lock
Use of shutter SQLite
洞态在某互联⽹⾦融科技企业的最佳落地实践
Machine learning - performance metrics
Some summary of pyqt5 learning (overview of the general meaning of some signals and methods)
2. Sensor size "recommended collection"
8 popular recommended style layout
孔松(信通院)-数字化时代云安全能力建设及趋势
Three questions about scientific entrepreneurship: timing, pain points and important decisions
关于佛萨奇2.0“Meta Force原力元宇宙系统开发逻辑方案(详情)
String input function
8款最佳实践,保护你的 IaC 安全!
Global and Chinese polypropylene industry prospect analysis and market demand forecast report Ⓝ 2022 ~ 2027
Colorful five pointed star SVG dynamic web page background JS special effect
Computer network interview knowledge points