#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
int data;
struct node *lchild,*rchild;
};
struct node *creat(int n,char *a,char *b)
{
struct node *root;
char *p;
if(n==0)
return NULL;
root=(struct node *)malloc(sizeof(struct node));
root->data=a[0];
for(p=b; p!='\0'; p++)
if(*p==a[0])
break; //寻找中序遍历中的相同结点,将数分成左子树和右子数
int t;
t=p-b;
root->lchild=creat(t,a+1,b); //左子树
root->rchild=creat(n-t-1,a+t+1,p+1); //右子树
return root;
}
void houxu(struct node *root)
{
if(root)
{
houxu(root->lchild);
houxu(root->rchild);
printf("%c",root->data);
}
}
int main()
{
int m;
struct node *root;
root=(struct node *)malloc(sizeof(struct node));
char a[100],b[100];
scanf("%s %s",a,b);
m=strlen(a);
root=creat(m,a,b);
houxu(root);
printf("\n");
}
二叉树:根据二叉树的先序遍历序列和中序遍历序列,输出该二叉树的后序遍历序列
最新推荐文章于 2021-02-23 08:50:48 发布