#include<stdio.h>#include<stdlib.h>/*递归前中后遍历*/typedefstructnode{intdata;structnode*left;structnode*right;}BTnode;BTnode*CreateTree(inta[],intn){BTnode*root,*c,*p,*pa;inti;root=(BTnode*)malloc(sizeof(BTnode));root->data=a[0];root->left=root->right=NULL;//建立根节点for(i=1;i<n;i++){p=(BTnode*)malloc(sizeof(BTnode));p->data=a[i];p->left=p->right=NULL;c=root;//根节点给C指针while(c){//判断p结点时属于左子树还是右子树pa=c;//pa指针是p结点的父节点if(c->data>p->data)c=c->left;else//如果结点值右重复,则后面结点在右孩子上c=c->right;}if(pa->data>p->data)//p结点时父节点的左孩子还是右孩子pa->left=p;elsepa->right=p;}returnroot;}voidForder(BTnode*root){if(root){printf("%d",root->data);printf("\n");Forder(root->left);Forder(root->right);}}voidInorder(BTnode*root){if(root){Inorder(root->left);printf("%3d",root->data);printf("\n");Inorder(root->right);}}voidPorder(BTnode*root){if(root){Porder(root->left);Porder(root->right);printf("%6d",root->data);printf("\n");}}intmain(void){BTnode*root;int*a;intn;inti;printf("请输入n=");scanf("%d",&n);a=(int*)malloc(n*sizeof(int));printf("请输入数组a[]=");for(i=0;i<n;i++)scanf("%d",&a[i]);root=CreateTree(a,n);Forder(root);Inorder(root);Porder(root);}