struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};*/
//使用bfs层次遍历求解深度
class Solution {
public:
int TreeDepth(TreeNode* pRoot) {
if(!pRoot)return 0;//如果为空直接返回0
queue<TreeNode*>tree;
int depth=0;
tree.push(pRoot);
while(!tree.empty()){
int size=tree.size();//通过队列每次只遍历一层
while(size--){
TreeNode* temp=tree.front();//队首出队
tree.pop();
if(temp->left!=NULL)
tree.push(temp->left);
if(temp->right!=NULL)
tree.push(temp->right);
}
depth+=1;
}
return depth;
}
};