/* * description:层次遍历 * writeby: nick * date: 2012-10-22 23:56 */ #include <iostream> #include <queue> using namespace std; struct node { int item; node *l, *r; node(int n) { item=n; l=0; r=0; } }; typedef node *link; void traverse(link h, void visit(link)) { queue<link> q; q.push(h); while(!q.empty()) { h = q.front(); q.pop(); visit(h); if(h->l != 0) q.push(h->l); if(h->r !=0) q.push(h->r); } } void visit(link p) { cout << p->item << " "; } int main() { link root = new node(4); root->l = new node(5); root->r = new node(6); root->l->l = new node(7); root->l->r = new node(8); cout << "中文"; traverse(root, visit); return 0; }