解题思路:
- 递归,当==nullptr的时候返回0,maxdepth(根节点)= 左右两个子树的深度的较大值+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 == nullptr) return 0;
return max(maxDepth(root->left), maxDepth(root->right))+1;
}
};