104. 二叉树的最大深度
二刷
class Solution {
public:
int maxDepth(TreeNode* root) {
if (!root)
return 0;
int DepthL = maxDepth(root->left) + 1;
int DepthR = maxDepth(root->right) + 1;
return std::max(DepthL, DepthR);
}
};
没什么好说的。官解有点细微差别,是先 max() 再 +1 的。
class Solution {
public:
int maxDepth(TreeNode* root) {
if (root == nullptr) return 0;
return max(maxDepth(root->left), maxDepth(root->right)) + 1;
}
};
三刷
class Solution {
public:
int maxDepth(TreeNode* root) {
if (!root)
return 0;
return max(maxDepth(root->left) + 1, maxDepth(root->right) + 1);
}
};
跟二刷基本一样。