LeetCode 104

https://leetcode.cn/problems/maximum-depth-of-binary-tree/description/

难度:简单

递归求最大深度。

时间复杂度:O(n),其中 n 为二叉树的节点个数。

空间复杂度:O(n)。最坏情况下,二叉树退化成一条链,递归需要 O(n) 的栈空间。

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