Description
读取字符串abcdefghij,然后层次建树建立一颗二叉树,然后中序遍历输出 hdibjeafcg,后序遍历输出 hidjebfgca,层序遍历输出abcdefghij,注意不要输出汉字
Input
abcdefghij
Output
中序遍历输出hdibjeafcg,后序遍历输出 hidjebfgca,层序遍历输出abcdefghij,每个一行
Sample Input 1
abcdefghij
Sample Output 1
hdibjeafcg hidjebfgca abcdefghij
#define _CRT_SECURE_NO_WARNINGS #include#include typedef char BiElemtype; typedef struct BiTNode { BiElemtype c; struct BiTNode* lchild; struct BiTNode* rchild; }BiTNode,*BiTree; typedef struct tag { BiTree p; tag* next; }tag_t,*ptag_t; void PreOrder(BiTree p) { if (p != NULL) { putchar(p->c); PreOrder(p->lchild); PreOrder(p->rchild); } } void InOrder(BiTree p) { if (p != NULL) { InOrder(p->lchild); putchar(p->c); InOrder(p->rchild); } } void PostOrder(BiTree p) { if (p != NULL) { PostOrder(p->lchild); PostOrder(p->rchild); putchar(p->c); } } void LevelOrder(ptag_t phead) { while (phead != NULL) { putchar(phead->p->c); phead = phead->next; } } int main() { char c; BiTree tree = NULL; BiTree pnew = NULL; ptag_t phead = NULL; ptag_t ptail = NULL; ptag_t pcur = NULL; ptag_t listpnew = NULL; while (scanf("%c", &c) != EOF) { if (c == 'n') { break; } pnew = (BiTree)calloc(1, sizeof(BiTNode)); pnew->c = c; listpnew = (ptag_t)calloc(1, sizeof(tag_t)); listpnew->p = pnew; if (tree == NULL) { tree = pnew; phead = ptail = pcur = listpnew; continue; } else { ptail->next = listpnew; ptail = listpnew; } if (pcur->p->lchild == NULL) { pcur->p->lchild = pnew; } else if (pcur->p->rchild == NULL) { pcur->p->rchild = pnew; pcur = pcur->next; } } InOrder(tree); printf("n"); PostOrder(tree); printf("n"); LevelOrder(phead); return 0; }



