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.
Subscribe to see which companies asked this question
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
}
int maxDepth(struct TreeNode* root) {
int count=0;
int h1,h2;
if(!root)//空树
return 0;
h1=maxDepth(root->left);
h2=maxDepth(root->right);
return h1>h2?h1+1:h2+1;
}