🧩Algo 二叉树

104. 二叉树的最大深度

难度:🟢 Easy | 标签:树、递归 | 状态:⬜ LeetCode 链接

思路

递归:max(left, right) + 1。 空树深度 0。

代码

class Solution {
public:
    int maxDepth(TreeNode* root) {
        if (!root) return 0;
        return max(maxDepth(root->left), maxDepth(root->right)) + 1;
    }
};

复杂度

  • 时间 O(n),空间 O(h)

易错 / 回顾

  • 迭代版用 BFS 层数
  • 最小深度(111)有坑:单子需要走到叶子
Related · 二叉树