1、中序,后序重构二叉树
struct node *rebuild(char *a, char *b, int n)//a为已知中序,b为已知后序
{
struct node *root;
int i;
if(n==0) return NULL;
root = (struct node*)malloc(sizeof(struct node));
root->data = b[n-1];
for(i = 0; i<n; i++)
{
if(a[i] == b[n-1]) break;
}
root->lchild = rebuild(a ,b, i);
root->rchild = rebuild(a+i+1, b+i, n-i-1);
return root;
}
2、前序,中序重构二叉树
struct node *rebuild(char *a, char *b, int n)//a为已知前序, b为已知中序
{
struct node *root;
int i;
if(n==0) return NULL;
root = (struct node*)malloc(sizeof(struct node));
root->data = a[0];
for(i = 0; i<n; i++)
{
if(b[i] == a[0]) break;
}
root->lchild = rebuild(a+1,b, i);
root->rchild = rebuild(a+i+1, b+i+1, n-i-1);
return root;
}
待更新