Leetcode 104. 二叉树的最大深度

文章目录

  • 题目
  • 代码(首刷自解)
  • 代码(8.14 二刷自解)
  • 代码(9.7 三刷自解)

题目

Leetcode 104. 二叉树的最大深度_第1张图片

Leetcode 104. 二叉树的最大深度

代码(首刷自解)

22年1月刷过,到现在都有印象,所以这个模板直接写出来了哈哈

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

代码(8.14 二刷自解)

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

代码(9.7 三刷自解)

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

你可能感兴趣的:(Leetcode专栏,leetcode,算法)