现在的位置: 首页 > 综合 > 正文

队列实现 二叉树的宽度优先遍历

2018年05月28日 ⁄ 综合 ⁄ 共 442字 ⁄ 字号 评论关闭

struct BinaryTreeNode{
    int m_value;
    BinaryTreeNode *leftTree;
    BinaryTreeNode *rightTree;
};

 

void printBinaryTree_BFS(BinaryTreeNode *root){
    queue<BinaryTreeNode*> Q;
    Q.push(root);
    while(!Q.empty()){
        BinaryTreeNode *temp = Q.front();
        cout<<temp->m_value<<endl;
        if(temp->leftTree!=NULL){
            Q.push(temp->leftTree);
        }
        if(temp->rightTree!=NULL){
            Q.push(temp->rightTree);
        }
        Q.pop();
    }
}

抱歉!评论已关闭.