| 
 | 
 
 
 楼主 |
发表于 2013-12-8 10:54:17
|
显示全部楼层
 
 
 
- #include <stdio.h>  
 
 - #include <malloc.h>  
 
 - #include <string.h>  
 
  
- typedef struct node  
 
 - {  
 
 -       char ch;  
 
 -       struct node *left, *right;  
 
 - }node;                
 
  
- node * creat(char *pre,char *in,int len); 
 
 - {
 
 -       int k;  
 
 -       if(len<=0) 
 
 -         return NULL;  
 
 -       node *head=(node*)malloc(sizeof(node));  
 
 -       head->ch=*pre;  
 
 -       char *p;  
 
 -       for(p=in;p!=NULL;p++) 
 
 -       {
 
 -             if(*p == *pre) 
 
 -             break;
 
 -       }                 
 
 -       k=p-in;  
 
 -       head->left=creat(pre+1,in,k); 
 
 -       head->right=creat(pre+k+1,p+1,len-k-1);  
 
 -       return head;  
 
 - }
 
  
- void print(node *head);  
 
 - {
 
 -         if(head==NULL) return ;  
 
 -   print(head->left);  
 
 -   print(head->right);  
 
 -   printf("%c",head->ch);
 
 - }
 
  
- int main()  
 
 - {  
 
 -   int len;
 
 -   int i;
 
 -   char pre[30],in[30];
 
 -   node *head;
 
 -   head=(node*)malloc(sizeof(node));
 
 -   while ((scanf("in:%spre:%s", in, pre) != EOF)
 
 -   {
 
 -       len=strlen(pre);  
 
 -       head=creat(pre,in,len);
 
 -       printf("post:");
 
 -       print(head);  
 
 -   }   
 
 -   return 0;  
 
 - }
 
  复制代码 |   
 
 
 
 |