int maxDepth(struct TreeNode* root){
if(root==NULL)
{
return 0;
}
else if(root->left==NULL&&root->right==NULL)
{
return 1;
}
else
{
int left=maxDepth(root->left);
int right=maxDepth(root->right);
return left>right?left+1:right+1;
}
}
在做这道题的时候我的一个思路是将最后的返回值写成
return (maxDepth(root->left)>maxDepth(root->right)?maxDepth(root->left):maxDepth(root->right))+1;