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);
}
};
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;
}
};
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);
}
};