#include<stdio.h> #include<malloc.h> typedef struct tree{ int data; //存放数据 struct tree *right,*left;//定义左右子树 }*t; void Create_tree(t &T,int a) { if(T==NULL) //根节点 { T=(t)malloc(sizeof(t));//分配空间 T->right=NULL; T->left=NULL; T->data=a; } else{ //子节点 if(a>T->data) //大的排在右边 { Create_tree(T->right,a); } if(a<T->data) //小的排在左边 { Create_tree(T->left,a); } else{ return; } } } void PreOrder(t T) //先序遍历 { if(T==NULL) return; printf("%d ",T->data); PreOrder(T->left); PreOrder(T->right); } void midOrder(t T) //中序遍历 { if(T==NULL) return; midOrder(T->left); printf("%d ",T->data); midOrder(T->right); } void FinOrder(t T) //后序遍历 { if(T==NULL) return; FinOrder(T->left); FinOrder(T->right); printf("%d ",T->data); } int main() { int n; while(scanf("%d",&n)!=EOF) //节点个数 { t T=NULL; int i,a; while(n--) { scanf("%d",&a); Create_tree(T,a); //创建排序二叉树 } PreOrder(T); printf("\n"); midOrder(T); printf("\n"); FinOrder(T); printf("\n"); } return 0; }