typedef struct Node {
char a;
struct Node *lchild;
struct Node *rchild;
}BiNode, *BiTree;
void PosTraverse(BiTree T) {
if (T == NULL) return;
PosTraverse(T->lchild);
PosTraverse(T->rchild);
printf("%c", T->a);
}
void BuildTree(char *pre, char* mid, BiTree root) {
int len = strlen(pre);
if (0 == len) return;
char *p = strchr(mid, pre[0]);
int pos = p - mid;
root->a = pre[0];
root->lchild = root->rchild = NULL;
if (pos != 0) {
BiTree left;
root->lchild = (BiTree)malloc(sizeof(BiNode));
left = root->lchild;
char *left_pre = (char*)malloc(sizeof(char) * (pos + 1));
char *left_mid = (char*)malloc(sizeof(char) * (pos + 1));
strncpy(left_pre, pre + 1, pos);
strncpy(left_mid, mid, pos);
left_pre[pos] = 0;
left_mid[pos] = 0;
BuildTree(left_pre, left_mid, left);
}
if (pos != len - 1) {
BiTree right;
root->rchild = (BiTree)malloc(sizeof(BiNode));
right = root->rchild;
char *right_pre = (char*)malloc(sizeof(char) * (len - pos));
char *right_mid = (char*)malloc(sizeof(char) * (len - pos));
strncpy(right_pre, pre + 1 + pos, len - pos - 1);
strncpy(right_mid, mid + 1 + pos, len - pos - 1);
right_pre[len - pos - 1] = 0;
right_mid[len - pos - 1] = 0;
BuildTree(right_pre, right_mid, right);
}
}
int main() {
BiNode temp;
BuildTree("abcdefg", "cbdafge", &temp);
PosTraverse(&temp);
}
关注此问题