当前位置:网站首页>【 剑指 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);
}
};
边栏推荐
- Report on the "14th five year plan" and scale prospect prediction of China's laser processing equipment manufacturing industry Ⓢ 2022 ~ 2028
- During Oracle CDC data transmission, the CLOB type field will lose its value during update. There is a value before update, but
- JS discolored Lego building blocks
- 龙蜥社区开源 coolbpf,BPF 程序开发效率提升百倍
- SAP 智能机器人流程自动化(iRPA)解决方案分享
- leetcode 322. Coin change (medium)
- Report on the 14th five year plan and future development trend of China's integrated circuit packaging industry Ⓓ 2022 ~ 2028
- Have you ever encountered the problem that flynk monitors the PostgreSQL database and checkpoints cannot be used
- Self cultivation of open source programmers who contributed tens of millions of lines of code to shardingsphere and later became CEO
- 1553B环境搭建
猜你喜欢
刘对(火线安全)-多云环境的风险发现
What is the future development direction of people with ordinary education, appearance and family background? The career planning after 00 has been made clear
[241. Design priority for operation expression]
Self cultivation of open source programmers who contributed tens of millions of lines of code to shardingsphere and later became CEO
Reasons for MySQL reporting 1040too many connections and Solutions
启动solr报错The stack size specified is too small,Specify at least 328k
04-Redis源码数据结构之字典
Chen Yu (Aqua) - Safety - & gt; Cloud Security - & gt; Multicloud security
Qtdeisgner, pyuic detailed use tutorial interface and function logic separation (nanny teaching)
5. Use of ly tab plug-in of header component
随机推荐
Listen in the network
Wave animation color five pointed star loader loading JS special effects
Some summary of pyqt5 learning (overview of the general meaning of some signals and methods)
Self cultivation of open source programmers who contributed tens of millions of lines of code to shardingsphere and later became CEO
Cs5268 advantages replace ag9321mcq typec multi in one docking station scheme
Professor Li Zexiang, Hong Kong University of science and technology: I'm wrong. Why is engineering consciousness more important than the best university?
04-Redis源码数据结构之字典
MySQL Replication中的并行复制示例详解
Asp.netcore利用dynamic简化数据库访问
Report on the 14th five year plan and future development trend of China's integrated circuit packaging industry Ⓓ 2022 ~ 2028
ArrayList扩容机制以及线程安全性
受益互联网出海 汇量科技业绩重回高增长
When you really learn databinding, you will find "this thing is really fragrant"!
The 14th five year plan of China's environmental protection industry and the report on the long-term goals for 2035 Ⓖ 2022 ~ 2028
5. Use of ly tab plug-in of header component
Anti fraud, refusing to gamble, safe payment | there are many online investment scams, so it's impossible to make money like this
word2vec训练中文词向量
Machine learning - performance metrics
Blind box NFT digital collection platform system development (build source code)
8 popular recommended style layout