面试常考手写代码之--二叉树层次遍历(BFS)

 

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);

        }

    }

}

你可能感兴趣的:(面试常考手写代码之--二叉树层次遍历(BFS))