二叉树的深度
二叉树的深度
http://www.nowcoder.com/questionTerminal/435fb86331474282a3499955f0a41e8b
非递归解法,利用队列
class Solution {
public:
int TreeDepth(TreeNode* pRoot)
{
if(!pRoot){
return 0;
}
queue<TreeNode*> q;
int depth = 0;
q.push(pRoot);
while(!q.empty()){
int len = q.size();
while(len--){
TreeNode* tmp = q.front();
q.pop();
if(len == 0){
depth++;
}
if(tmp->left){
q.push(tmp->left);
}
if(tmp->right){
q.push(tmp->right);
}
}
}
return depth;
}
};

