码迷,mamicode.com
首页 > 其他好文 > 详细

二叉树6----一层二叉树的遍历

时间:2015-06-06 14:52:00      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:

1、二叉树定义

typedef struct BTreeNodeElement_t_ {
    void *data;
} BTreeNodeElement_t;

typedef struct BTreeNode_t_ {
    BTreeNodeElement_t *m_pElemt;
    struct BTreeNode_t_    *m_pLeft;
    struct BTreeNode_t_    *m_pRight;
} BTreeNode_t;


2、按层遍历二叉树


第一步:须要借助队列,首先将根节点pRoot入队;

第二步:当队列不空时,获得队首元素并出队,赋给pRoot,运行第三步;

第三步:假设pRoot左节点存在,则入队;假设pRoot右节点存在,则入队;运行第二步。


void  LevelTraverse( BTreeNode_t *pRoot){
    if( pRoot == NULL )
        return ;

    queue <BTreeNode_t *>  que;
    que.push( pRoot);

    while( !que.empty() ){
        pRoot = que.front();
        que.pop();
        Visit( pRoot);

        if( pRoot->m_pLeft != NULL )
            que.push( pRoot->m_pLeft );
        if( pRoot->m_pRight != NULL )
            que.push( pRoot->m_pRight);
    }

    return ;
}


二叉树6----一层二叉树的遍历

标签:

原文地址:http://www.cnblogs.com/hrhguanli/p/4556604.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!