当前位置:网站首页>【 剑指 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);
}
};
边栏推荐
- LeetCode重建二叉树详解[通俗易懂]
- Analysis report on the development trend and prospect scale of silicon intermediary industry in the world and China Ⓩ 2022 ~ 2027
- minimum spanning tree
- B站被骂上了热搜。。
- Analysis report on the development prospect and investment strategic planning of China's wafer manufacturing Ⓔ 2022 ~ 2028
- 5G工业网关的科技治超应用 超限超重超速非现场联合执法
- Asp.netcore利用dynamic简化数据库访问
- Analysis report on the development trend and Prospect of new ceramic materials in the world and China Ⓐ 2022 ~ 2027
- Listen in the network
- 2022 · 让我带你Jetpack架构组件从入门到精通 — Lifecycle
猜你喜欢
![[241. Design priority for operation expression]](/img/72/29d27204d5213a8efdb2c5be925dec.png)
[241. Design priority for operation expression]

A Fletter version of Notepad

1553B environment construction

Computer network interview knowledge points

Explain IO multiplexing, select, poll, epoll in detail

SAP intelligent robot process automation (IRPA) solution sharing

1553B环境搭建

MySQL 66 questions, 20000 words + 50 pictures in detail! Necessary for review

Flow management technology

5G工业网关的科技治超应用 超限超重超速非现场联合执法
随机推荐
Qtdeisgner, pyuic detailed use tutorial interface and function logic separation (nanny teaching)
1.8新特性-List
Introduction to topological sorting
Self cultivation of open source programmers who contributed tens of millions of lines of code to shardingsphere and later became CEO
Build a vc2010 development environment and create a tutorial of "realizing Tetris game in C language"
Camp division of common PLC programming software
微机原理与接口技术知识点整理复习–纯手打
洞态在某互联⽹⾦融科技企业的最佳落地实践
Have you ever encountered the problem that flynk monitors the PostgreSQL database and checkpoints cannot be used
Learning to use livedata and ViewModel will make it easier for you to write business
基于mysql乐观锁实现秒杀的示例代码
Meta enlarge again! VR new model posted on CVPR oral: read and understand voice like a human
Leetcode question 1: sum of two numbers (3 languages)
Several models of IO blocking, non blocking, IO multiplexing, signal driven and asynchronous IO
word2vec训练中文词向量
学历、长相、家境普通的人,未来的发展方向是什么?00后的职业规划都已经整得明明白白......
Summary of interview questions (1) HTTPS man in the middle attack, the principle of concurrenthashmap, serialVersionUID constant, redis single thread,
04 redis source code data structure dictionary
陈宇(Aqua)-安全->云安全->多云安全
minimum spanning tree