题目:
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.
程序:
class Solution {
public:
int maxDepth(TreeNode *root) {
if(root==NULL)
return 0;
int h1=0,h2=0;
if(root->left)
h1=maxDepth(root->left);
if(root->right)
h2=maxDepth(root->right);
return h1>h2?h1+1:h2+1;
}
};