题目描述
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
输入
/**
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
class Solution {
public:
/**
*
* @param root TreeNode类
* @return int整型
*/
int maxDepth(TreeNode* root) {
// write code here
if (root==NULL){return 0;}
queue<TreeNode*> que;
que.push(root);
int depth=0;
while (!que.empty()){
int size=que.size();
depth++;
for (int i=0;i<size;++i){
TreeNode *tmp=que.front();
que.pop();
if (tmp->left !=NULL)
que.push(tmp->left);
if (tmp->right !=NULL)
que.push(tmp->right);
}
}
return depth;
}
};