struct BTNode_s{
int value;
BTNode_s* pLeft;
BTNode_s* pRight;
}BTNode;
void BFS(BTNode* pRoot)
{
if(pRoot ==NULL)
return;
std::deque<BTNode*> treeDeque;
treeDeque.push_back(pRoot);
while(!treeDeque.empty())
{
BTNode* pNode =treeDeque.front();
printf("%d\t", pNode->value);
if(pNode->pLeft!= NULL)
{
treeDeque.push_back(pNode->pLeft);
}
if(pNode->pRight!= NULL)
{
treeDeque.push_back(pNode->pRight);
}
}
}
本文介绍了一种使用双端队列实现的二叉树广度优先遍历算法,并提供了详细的C++代码实现。该算法适用于任何类型的二叉树结构,能够有效地按层次顺序访问所有节点。
1万+

被折叠的 条评论
为什么被折叠?



