面试题23:二叉树层序遍历

这个简单

#include <queue>

void PrintFromTopToBottom(BinaryTreeNode* pRoot)
{
    if (pRoot == NULL)
        return;

    std::queue<BinaryTreeNode *> queueData;
    queueData.push(pRoot);
    while (!queueData.empty())
    {
        BinaryTreeNode *tempNode = queueData.front();
        printf("%d ", tempNode->m_nValue);
        queueData.pop();
        if (tempNode->m_pLeft != NULL)
            queueData.push(tempNode->m_pLeft);
        if (tempNode->m_pRight != NULL)
            queueData.push(tempNode->m_pRight);
    }
}

 

posted on 2016-07-07 11:23  已停更  阅读(170)  评论(0编辑  收藏  举报