二叉树非递归 层次遍历

 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 阅读(207) 评论(0)  编辑 收藏 引用 所属分类: C++代码


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2024年5月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

导航

统计

常用链接

留言簿

文章分类

文章档案

收藏夹

搜索

最新评论