解题思路:
注意事项:
参考代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct treenode
{
char data;
struct treenode* lchild;
struct treenode* rchild;
} Tree;
Tree* creattree(char* pre, char* in, int n)
{
Tree* root;
int i;
if (n <= 0)
return NULL;
root = (Tree*)malloc(sizeof(Tree));
if (root == NULL)
return NULL;
root->data = pre[0];
for (i = 0; i < n; i++)
{
if (pre[0] == in[i])
break;
}
root->lchild = creattree(pre + 1, in, i);
root->rchild = creattree(pre + i + 1, in + i + 1, n - i - 1);
return root;
}
void posttraverse(Tree* root)
{
if (root != NULL)
{
posttraverse(root->lchild);
posttraverse(root->rchild);
printf("%c", root->data);
}
}
int main()
{
char pre[30], in[30];
while (scanf("%s%s", pre, in) != EOF)
{
Tree* root;
int n = strlen(pre);
root = creattree(pre, in, n);
posttraverse(root);
printf("\n");
}
return 0;
}
0.0分
0 人评分
C语言程序设计教程(第三版)课后习题5.8 (C语言代码)浏览:567 |
WU-图形输出 (C++代码)浏览:800 |
WU-格式化数据输出 (C语言代码)浏览:1742 |
WU-小九九 (C++代码)浏览:1681 |
【明明的随机数】 (C语言代码)浏览:785 |
字符逆序 (C语言代码)浏览:609 |
剪刀石头布 (C语言代码)浏览:1432 |
第三届阿里中间件性能挑战赛-总决赛亚军比赛攻略浏览:1144 |
C语言程序设计教程(第三版)课后习题10.7 (用指针求解)浏览:1458 |
简单的a+b (C语言代码)浏览:531 |