C++速通LeetCode简单第9题-二叉树的最大深度

深度优先算法递归:

cpp 复制代码
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int maxDepth(TreeNode* root) {
        if(root == nullptr) return 0;
        return max(maxDepth(root->left), maxDepth(root->right)) + 1;
    }
};
相关推荐
CC数学建模5 分钟前
2026年第十六届APMCM 亚太地区大学生数学建模竞赛(中文赛项)赛题C题:创业社区规划与资源配置优化问题完整思路、代码、模型、文章,全网首发高质量分享!
python·算法·数学建模
徐小夕8 分钟前
我们放弃了单Agent方案:HiCAD 3.0 用 Harness 做多Agent编排,把3D建模的准确率提升了30%
前端·算法·github
洛水水12 分钟前
【力扣100题】88.多数元素
数据结构·算法·leetcode
alwaysrun16 分钟前
C++之常量体系const
c++·后端·程序员
郝学胜_神的一滴17 分钟前
CMake 016:深入浅出变量核心用法
c++·cmake
Shan120518 分钟前
无向图的Hierholzer算法流程(一)
算法
一切皆是因缘际会22 分钟前
频域特征解构底层机理与双域融合鉴伪算法优化
人工智能·算法·ai·架构
学逆向的23 分钟前
C++模板
开发语言·c++·网络安全
Smilecoc25 分钟前
决策树(三):剪枝
算法·决策树·剪枝
bIo7lyA8v29 分钟前
算法性能建模的数值方法与误差分析的技术8
算法