C语言实现:

```/**
* Definition for a binary tree node.
* struct TreeNode {
*     int val;
*     struct TreeNode *left;
*     struct TreeNode *right;
* };
*/

int maxDepth(struct TreeNode* root)
{
if(NULL == root)
{
return 0;
}

int llen = maxDepth(root->left) + 1;
int rlen = maxDepth(root->right) + 1;

return (llen > rlen)? llen :rlen;
}
```

C++实现

``````/**
* Definition for a binary tree node.
* struct TreeNode {
*     int val;
*     TreeNode *left;
*     TreeNode *right;
*     TreeNode() : val(0), left(nullptr), right(nullptr) {}
*     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
*     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int maxDepth(TreeNode* root) {
int level = 0;
queue<TreeNode *> queue;
if(root) queue.push(root);
while(!queue.empty())
{
level += 1;
int n = queue.size();
for(int i = 0;i < n;i++)
{
TreeNode * elem = queue.front();
if(elem->left) queue.push(elem->left);
if(elem->right) queue.push(elem->right);
queue.pop();
}
}

return level;
}
};``````

``````Runtime: 8 ms, faster than 92.88% of C++ online submissions for Maximum Depth of Binary Tree.
Memory Usage: 19.2 MB, less than 16.22% of C++ online submissions for Maximum Depth of Binary Tree.``````

``````/**
* Definition for a binary tree node.
* struct TreeNode {
*     int val;
*     TreeNode *left;
*     TreeNode *right;
*     TreeNode() : val(0), left(nullptr), right(nullptr) {}
*     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
*     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int maxDepth(TreeNode* root) {
if(root == NULL)
return 0;
return 1 + max(maxDepth(root->left),maxDepth(root->right));
}
};``````

``````Runtime: 8 ms, faster than 92.88% of C++ online submissions for Maximum Depth of Binary Tree.
Memory Usage: 19.6 MB, less than 16.22% of C++ online submissions for Maximum Depth of Binary Tree.``````

``````/**
* Definition for a binary tree node.
* struct TreeNode {
*     int val;
*     TreeNode *left;
*     TreeNode *right;
*     TreeNode() : val(0), left(nullptr), right(nullptr) {}
*     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
*     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int maxDepth(TreeNode* root) {
if(root == NULL)
return 0;

int left = maxDepth(root->left);
int right = maxDepth(root->right);

return 1 + (left > right) ? left:right;
}
};``````