方法一:深度优先遍历
class Solution {
public:
int maxDepth(TreeNode *root) {
if(root==NULL)
return 0;
if(root->left==NULL&&root->right==NULL)
return 1;
int mx=0;
if(root->left)
mx=maxDepth(root->left)+1;
if(root->right)
mx=max(maxDepth(root->right)+1, mx);
return mx;
}
};