层次遍历二叉树

时间:2022-08-01 17:26:53

层次遍历二叉树,即每一层从左到右遍历二叉树,而队列是先进先出的顺序,这里我们使用双端队列实现层次遍历二叉树。

-(void)FloorVist(BinTree *root) {

        if(root == NULL) {
             return;
        } else {
            deque<BinTree*>deque;
            dequ.push_back(root);
            BinTree *cur = NULL;
           //开始遍历
           while(dequ.size()){
                cur = dequ.front();
                dequ.pop_front();
                count<<cur->value<<endl;
               if(cur->left != NULL)
               dequ.push_back(cur->left);
               if(cur->right != NULL)
               dequ.push_back(cur->right);
            }
        }
}