Time Limit: 2000/1000 MS
(Java/Others) Memory Limit: 32768/32768 K
(Java/Others)
Total Submission(s): 917 Accepted
Submission(s): 496
1 /* 2 4 3 1 3 4 2 4 5 1 3 2 4 6 */ 7 #include<iostream> 8 #include<stdio.h> 9 #include<cstring> 10 #include<cstdlib> 11 using namespace std; 12 13 struct node 14 { 15 int rp; 16 struct node *lchild; 17 struct node *rchild; 18 }; 19 20 int n,len; 21 22 void mem(struct node *p) 23 { 24 p->rp=0; 25 p->lchild=NULL; 26 p->rchild=NULL; 27 } 28 void insert_ecs(struct node **p,int x) 29 { 30 if((*p)==NULL) 31 { 32 (*p)=(struct node*)malloc(sizeof(struct node)); 33 mem(*p); 34 (*p)->rp=x; 35 return; 36 } 37 if( (*p)->rp > x) 38 insert_ecs( &(*p)->lchild,x); 39 else insert_ecs( &(*p)->rchild,x); 40 } 41 void serch(struct node *p) 42 { 43 printf("%d",p->rp); 44 len++; 45 if(len!=n) printf(" ",p->rp); 46 else printf("\n"); 47 48 if( p->lchild!=NULL ) 49 serch(p->lchild); 50 if( p->rchild!=NULL ) 51 serch(p->rchild); 52 } 53 void deal(struct node *p) 54 { 55 if(p->lchild!=NULL) 56 deal(p->lchild); 57 if(p->rchild!=NULL) 58 deal(p->rchild); 59 free(p); 60 } 61 int main() 62 { 63 int i,x; 64 while(scanf("%d",&n)>0) 65 { 66 struct node *root=NULL; 67 for(i=1;i<=n;i++) 68 { 69 scanf("%d",&x); 70 insert_ecs(&root,x); 71 } 72 len=0; 73 serch(root); 74 free(root); 75 } 76 return 0; 77 }
原文:http://www.cnblogs.com/tom987690183/p/3574772.html