/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};*/
class Solution {
public:
int TreeDepth(TreeNode* pRoot) {
int l = 0;
if (!pRoot) return 0;
deque<TreeNode*> q;
q.push_back(pRoot);
while(!q.empty())
{
int size = q.size();
l++;
while(size--)
{
TreeNode* p = q.front();
q.pop_front();
if(p->left) q.push_back(p->left);
if(p->right) q.push_back(p->right);
}
}
return l;
}
};
剑指数据结构-二叉树的深度
最新推荐文章于 2022-07-16 15:30:29 发布