# 【LeetCode刷题（简单程度）】剑指 Offer 55 - I. 二叉树的深度

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

int travelevel(TreeNode* root)
{
int depth = 0;
if(!root)
return 0;
queue<TreeNode*> q;
q.push(root);

while(!q.empty())
{
int size = q.size();
for(int i = 0;i < size;++i)
{
TreeNode* cur = q.front();
q.pop();
if(cur->left)
q.push(cur->left);
if(cur->right)
q.push(cur->right);
}
depth++;
}
return depth;
}
};
``````

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

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