输入样例:
1248009005003600700
0
输出样例:
89567
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX_SIZE 100
#define LNode BiTree
typedef struct BiTNode {
char data;
struct BiTNode *left, *right;
} BiTNode, *BiTree;
void CreateBiTree(BiTree *T, char *str, int *cursor);
void DestroyBiTree(BiTree T);
void LinkLeafNode(LNode T, BiTNode **last_leaf);
void ListShow(LNode T);
void ListRestore(LNode T);
int main() {
char str[MAX_SIZE];
while (scanf("%s", str) && str[0] != 48) {
BiTree T = NULL;
int cursor = 0;
LNode head_node = (BiTree) malloc(sizeof(BiTNode));
LNode head = head_node;
CreateBiTree(&T, str, &cursor);
LinkLeafNode(T, &head_node);
ListShow(head->right);
ListRestore(head);
DestroyBiTree(T);
free(head);
}
return 0;
}
void CreateBiTree(BiTree *T, char *str, int *cursor) {
if (str[*cursor] == 48) {
*T = NULL;
} else {
*T = (BiTree) malloc(sizeof(BiTNode));
(*T)->data = str[*cursor];
(*cursor)++;
CreateBiTree(&((*T)->left), str, cursor);
(*cursor)++;
CreateBiTree(&((*T)->right), str, cursor);
}
}
void DestroyBiTree(BiTree T) {
if (T) {
DestroyBiTree(T->left);
DestroyBiTree(T->right);
free(T);
}
}
void LinkLeafNode(LNode T, BiTNode **last_leaf) {
if (T) {
if (!T->left && !T->right) {
(*last_leaf)->right = T;
T->left = *last_leaf;
*last_leaf = T;
} else {
LinkLeafNode(T->left, last_leaf);
LinkLeafNode(T->right, last_leaf);
}
}
}
void ListShow(LNode T) {
while (T) {
printf("%c", T->data);
T = T->right;
}
}
void ListRestore(LNode T) {
LNode next;
while (T) {
next = T->right;
T->right = NULL;
T->left = NULL;
T = next;
}
}