解题思路:
注意事项:
参考代码:
#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语言程序设计教程(第三版)课后习题7.2 (C语言代码)浏览:1073 |
永远的丰碑 (C语言代码)浏览:656 |
分糖果 (C++代码)浏览:1450 |
【亲和数】 (C语言代码)浏览:860 |
Pascal三角 (C语言代码)格式错误浏览:521 |
A+B for Input-Output Practice (V) (C语言代码)浏览:625 |
C二级辅导-阶乘数列 (C语言代码)浏览:692 |
WU-printf基础练习2 (C++代码)浏览:2007 |
简单的a+b (C语言代码)浏览:528 |
C语言程序设计教程(第三版)课后习题7.2 (C语言代码)浏览:799 |