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;
    }
};
相关推荐
charlie11451419111 分钟前
Windows 10 系统编程——线程专题1
c++·windows·学习·线程
夏鹏今天学习了吗19 分钟前
【LeetCode热题100(35/100)】LRU 缓存
算法·leetcode·缓存
拾光Ծ41 分钟前
【C++】STL有序关联容器的双生花:set/multiset 和 map/multimap 使用指南
数据结构·c++·算法
澄澈i1 小时前
设计模式学习[20]---桥接模式
c++·学习·设计模式·桥接模式
西望云天2 小时前
The 2023 ICPC Asia Shenyang Regional Contest(2023沈阳区域赛CEJK)
数据结构·算法·icpc
我星期八休息2 小时前
C++异常处理全面解析:从基础到应用
java·开发语言·c++·人工智能·python·架构
zh_xuan2 小时前
LeeCode92. 反转链表II
数据结构·算法·链表·leecode
2401_841495642 小时前
【数据结构】汉诺塔问题
java·数据结构·c++·python·算法·递归·
Q741_1473 小时前
C++ 位运算 高频面试考点 力扣137. 只出现一次的数字 II 题解 每日一题
c++·算法·leetcode·面试·位运算