Description
读取字符串abcdefghij,然后层次建树建立一颗二叉树,然后前序遍历输出abdhiejcfg,注意不要打印前序遍历几个汉字
Input
abcdefghij
Output
abdhiejcfg
Sample Input 1
abcdefghij
Sample Output 1
abdhiejcfg
#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; struct tag* pnext; }tag_t,*ptag_t; void PreOrder(BiTree p) { if (NULL != p) { putchar(p->c); PreOrder(p->lchild); PreOrder(p->rchild); } } int main() { BiTree tree = NULL; BiTree pnew; ptag_t phead = NULL; ptag_t ptail = NULL; ptag_t pcur = NULL; ptag_t listpnew; char c; 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)); listpnew->p = pnew; if (tree == NULL) { tree = pnew; pcur = phead = ptail = listpnew; continue; } else { ptail->pnext = listpnew; ptail = listpnew; } if (NULL == pcur->p->lchild) { pcur->p->lchild = pnew; } else if(NULL == pcur->p->rchild) { pcur->p->rchild = pnew; pcur = pcur->pnext; } } PreOrder(tree); }



