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.
题意为:输出一个二叉树的深度
实现起来很容易。Accepted
用C代码实现如下:
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
int maxDepth(struct TreeNode *root) {
int LD,RD;
if(root==NULL)
return 0 ;
else{
LD = maxDepth(root->left); // 左子树的深度
RD = maxDepth(root->right); // 右子树的深度
return (LD>RD?LD:RD)+1; // 返回左、右子树深度的最大值+1,即整棵树的最大深度
}
}