Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct node node;
struct node{
int key;
node* left;
node* right;
};
node* t(char* pre,char* in,int st_pre,int en_pre,int st_in,int en_in);
int main ()
{
char pre[13]={'a','b','c','d','e','f','g','h','i','j','k','l','\0'};
char in[13]={'c','b','e','d','f','a','h','g','j','i','l','k','\0'};
node* root=t(pre,in,0,0,0,0);
return 0;
}
node* t(char* pre,char* in,int st_pre,int en_pre,int st_in,int en_in)
{
int i;
node* root=(node*)malloc(sizeof(node));
if (st_pre==en_pre)
return NULL;
root->key=pre[st_pre];
for(i=0;((i<(int)strlen(pre))&&(pre[st_pre]!=in[i]));i++)
{
}
root->left=t(pre,in,st_pre+1,st_pre+i+1,st_in,i-1);
root->right=t(pre,in,st_pre+i+1,(int)(strlen(pre)-1),i+1,(int)(strlen(pre)-1));
return root;
}