Given a binary tree, find its maximum depth.
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
递推算法 4ms AC
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
int maxDepth(struct TreeNode* root) {
if(root!=NULL)
{
int leftlength=maxDepth(root->left);
int rightlength=maxDepth(root->right);
return leftlength>rightlength? leftlength+1:rightlength+1;
}
else
return 0;
}