其中用到了队列 可以用一个循环队列替换 可以翻看之前的博文 ,
这次主要介绍了二叉树的四种遍历以及构建二叉树
#include#include #include using namespace std; typedef struct Tree { int data; struct Tree *lchild; struct Tree *rchild; }Tree; Tree* CreateTree() { Tree* T; T=(Tree*)malloc(sizeof(Tree)); int data1; scanf("%d",&data1); if(data1==-1) { return NULL; } T->data=data1; printf("Please input %d 's Left tree:n",data1); T->lchild=CreateTree(); printf("Please input %d 's Right tree:n",data1); T->rchild=CreateTree(); return T; } void FirstPrintTree(Tree *T) { if(T==NULL) { return ; } printf("%d ",T->data); FirstPrintTree(T->lchild); FirstPrintTree(T->rchild); } void MidnumPrintTree(Tree *T) { if(T==NULL) { return ; } MidnumPrintTree(T->lchild); printf("%d ",T->data); MidnumPrintTree(T->rchild); } void LastPrintTree(Tree *T) { if(T==NULL) { return ; } LastPrintTree(T->lchild); LastPrintTree(T->rchild); printf("%d ",T->data); } void LayerOrder(Tree *T) { queue q; q.push(T); while(!q.empty()) { Tree *flag; flag=q.front(); q.pop(); printf("%d ",flag->data); if(flag->lchild!=NULL) q.push(flag->lchild); if(flag->rchild!=NULL) q.push(flag->rchild); } } int main() { Tree *T; T=CreateTree(); FirstPrintTree(T); printf("n"); MidnumPrintTree(T); printf("n"); LastPrintTree(T); printf("n"); LayerOrder(T); printf("n"); system("pause"); return 0; }



