为什么这个树的层次遍历不对呢?想了半天了。。
程序代码:
#include <stdio.h> #include <malloc.h> struct tree{ char data; struct tree *lchild,*rchild; }; struct tree *create(struct tree *tree){ char ch; scanf(" %c",&ch); if(ch=='#') tree=NULL; else{ tree=(struct tree *)malloc(sizeof(struct tree)); tree->data=ch; tree->lchild=create(tree->lchild); tree->rchild=create(tree->rchild); } return tree; } void levelorder(struct tree *tree){ struct tree *a[100]; int rear=0,front=0; if(tree){ a[rear++]=tree; while(front!=rear){ printf("%2c",a[front]->data); if(a[front]->lchild) a[rear++]=tree->lchild; if(a[front]->rchild) a[rear++]=tree->rchild; front++; } } } void main(){ struct tree *t; printf("输入节点值(按照先序遍历输入)"); t=create(t); printf("按层遍历(队列):"); levelorder(t); }大家帮我看看吧 谢谢。