D / / B E / \ / \ A C G / / F
DBACEGF ABCDEFG BCAD CBAD
ACBFGED CDAB
# include <stdio.h> # include <stdlib.h> # include <string.h> typedef struct Node { char data; struct Node *Lchild,*Rchild; }*BiTree,BiTreenode; void CreateTree(char *pre, char *mid, BiTree &T, int len) { if (len == 0) { T = NULL; return ; } char ch = pre[0]; int index = 0; while (mid[index] != ch) index++; T = (BiTree)malloc(sizeof(BiTreenode)); T->data = ch; CreateTree(pre+1, mid, T->Lchild, index); CreateTree(pre+index+1, mid+index+1,T->Rchild, len-index-1); } void PrintTree(BiTree T) { if (T != NULL) { PrintTree(T->Lchild); PrintTree(T->Rchild); printf("%c",T->data); } } int main(void) { int len; char pre[27],mid[27]; while (~scanf("%s%s",pre,mid)) { len = strlen(pre); BiTree T=NULL; CreateTree(pre, mid, T, len); PrintTree(T); printf("\n"); } return 0; }
原文地址:http://blog.csdn.net/java_oracle_c/article/details/41927971