C語言源碼: [cpp] #include<stdio.h> #include<stdlib.h> #include<limits.h> typedef struct BiTree { int data; struct BiTree *lchild,*rchild; }BiTree; BiTree *create(BiTree *t,int n) { BiTree *p,*q; p=(BiTree *)malloc(sizeof(BiTree)); p->data=n; p->lchild=NULL; p->rchild=NULL; if(!t) t=p; else { q=t; while((n<q->data&&q->lchild)||(n>q->data&&q->rchild)) { if(n<=q->data) q=q->lchild; else q=q->rchild; } if(n<q->data) q->lchild=p; else if(n>q->data) q->rchild=p; } return t; } void Pre(BiTree *t) { if(t) { printf("%d ",t->data); Pre(t->lchild); Pre(t->rchild); } } void Mid(BiTree *t) { if(t) { Mid(t->lchild); printf("%d ",t->data); Mid(t->rchild); } } void Post(BiTree *t) { if(t) { Post(t->lchild); Post(t->rchild); printf("%d ",t->data); } } int main() { int n,i,x; BiTree *T; while(scanf("%d",&n)!=EOF) { T=NULL; for(i=1;i<=n;i++) { scanf("%d",&x); T=create(T,x); } Pre(T); printf("\n"); Mid(T); printf("\n"); Post(T); printf("\n"); } }