Problem Description
已知一个按先序输入的字符序列,如abd,,eg,,,cf,,,(其中,表示空结点)。请建立二叉树并求二叉树的层次遍历序列。
Input
输入数据有多行,第一行是一个整数t (t<1000),代表有t行测试数据。每行是
一个长度小于50个字符的字符串。
Output
输出二叉树的层次遍历序列。
Example Input
2 abd,,eg,,,cf,,, xnl,,i,,u,,
Example Output
abcdefg xnuli#include<stdio.h> #include<string.h> #include<stdlib.h> #define ERROR -1 #define OK 1 typedef char TElemtype; typedef int Statu; typedef struct BiTnode { TElemtype data; struct BiTnode *lchild, *rchild; }BiTnode, *BiTree; TElemtype str[55]; int i; Statu CreateBiTree(BiTree &T); Statu Ceng(BiTree T); int main() { BiTree T; int t; scanf("%d", &t); while(t--) { scanf("%s", str); i = 0; CreateBiTree(T); Ceng(T); printf("\n"); } return 0; } Statu CreateBiTree(BiTree &T) { if(str[i++] == ',') T = NULL; else { T = (BiTree)malloc(sizeof(BiTnode)); if(!T) exit(ERROR); T -> data = str[i - 1]; CreateBiTree(T -> lchild); CreateBiTree(T -> rchild); } return OK; } Statu Ceng(BiTree T) { if(T == NULL) return OK; int i = 0; int n = 0; BiTree a[10010]; a[n++] = T; while(i < n) { if(a[i]) { printf("%c", a[i] -> data); if(a[i] -> lchild) a[n++] = a[i] -> lchild; if(a[i] -> rchild) a[n++] = a[i] -> rchild; } i++; } return OK; }