二叉树的层序遍历

 

void visit(Node *pRoot)
{
    cout<<pRoot->value<<endl;
}

void LevelTraverse(Node *pRoot)
{
    if (!pRoot)
        return;

    queue<Node *> q;
    q.push(pRoot);
    while (!q.empty())
    {
        Node *pNode = q.front();
        q.pop();
        visit(pNode);
        if (pNode->pLeft)
            q.push(pNode->pLeft);
        if (pNode->pRight)
            q.push(pNode->pRight);
    }
}

 

 

 

 

 

 

EOF

posted on 2012-12-07 14:38  kkmm  阅读(213)  评论(0编辑  收藏  举报