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

来源:互联网 发布:忘记mac管理员密码 编辑:程序博客网 时间:2024/05/18 14:46

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

0 0
原创粉丝点击