您的位置:首页 > 其它

已知一棵二叉树的中序遍历和后序遍历,求二叉树的先序遍历

2017-02-22 21:03 483 查看
#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[n - 1];
for(p = b; p != '\0'; p++)
if(*p == a[n - 1])
break;
int t = p - b;
root -> lchild = creat(t, a, b);
root -> rchild = creat(n - t - 1, a + t, p + 1);
return root;
}
void xianxu(struct node *root)
{
if(root)
{
printf("%c",root->data);
xianxu(root->lchild);
xianxu(root->rchild);
}
}
int main()
{
int m, t;
scanf("%d", &t);
getchar();
while(t--)
{
struct node *root;
root=(struct node *)malloc(sizeof(struct node));
char a[100],b[100];
scanf("%s %s",b,a);
m=strlen(a);
root=creat(m,a,b);
xianxu(root);
printf("\n");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐