二叉树非递归 层次遍历

 1#include<iostream>
 2#include<queue>//队列
 3using namespace std;
 4
 5struct BinTreeNode{
 6    int data;
 7    BinTreeNode *left;
 8    BinTreeNode *right;
 9}
;
10void level(BinTreeNode *root){//层次遍历
11    queue<BinTreeNode*> q;
12    BinTreeNode *tmp = root;
13    q.push(tmp);
14    while(!q.empty()){
15        tmp = q.front();
16        cout<<tmp->data<<' ';
17        if(tmp->left != NULL)
18            q.push(tmp->left);
19        if(tmp->right != NULL)
20            q.push(tmp->right);
21        q.pop();
22    }

23}

24

posted on 2011-08-18 15:44 Hsssssss 阅读(231) 评论(0)  编辑 收藏 引用 所属分类: C++代码

<2025年5月>
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

导航

统计

常用链接

留言簿

文章分类

文章档案

收藏夹

搜索

最新评论