#include<iostream> #include<algorithm> using namespace std; #define maxn 10 int a[]={0,1,2,3,4,5,6,7,8,9}; struct node { int key; struct node * lchild,*rchild; }; node * create(int pos) { if(pos>=maxn) return NULL; node * root=(node*)malloc(sizeof(node)); root->key=a[pos]; root->lchild=create(2*pos); root->rchild=create(2*pos+1); return root; } void levelorder(node * root) { node * q[maxn]; int top,tail; top=0,tail=1; q[0]=root; while(top<tail) { printf("%d\n",q[top]->key); if(q[top]->lchild!=NULL) q[tail++]=q[top]->lchild; if(q[top]->rchild!=NULL) q[tail++]=q[top]->rchild; top++; } } int main() { levelorder(create(1));
}
|