#include <stdio.h>
#include<assert.h>
#include<stdlib.h>
typedef char BTDataType;
typedef struct BinaryTreeNode
{
BTDataType _data;
struct BinaryTreeNode* _left;
struct BinaryTreeNode* _right;
}BTNode;
BTNode* BuyNewBTNode(BTDataType x)
{
BTNode* newnode = (BTNode*)malloc(sizeof(BTNode));
if (newnode == NULL)
{
perror("malloc fail");
exit(-1);
}
newnode->_data = x;
newnode->_left = NULL;
newnode->_right = NULL;
return newnode;
}
// 通过前序遍历的数组"ABD##E#H##CF##G##"构建二叉树
BTNode* BinaryTreeCreate(BTDataType* a, int* pi)
{
assert(a);
if (a[*pi] == '#' || a[*pi] == '\0')
{
(*pi)++;//步进
return NULL;
}
BTNode* root = BuyNewBTNode(a[*pi]);
(*pi)++;//步进
root->_left = BinaryTreeCreate(a ,pi);
root->_right = BinaryTreeCreate(a , pi);
return root;
}
void BinaryTreeInOrder(BTNode* root)
{
if (root == NULL)
{
return;
}
BinaryTreeInOrder(root->_left);
printf("%c ", root->_data);
BinaryTreeInOrder(root->_right);
}
int main() {
char a[100];
while (scanf("%s", a) != EOF) { // 注意 while 处理多个 case
// 64 位输出请用 printf("%lld") to
int i = 0;
BTNode* root= BinaryTreeCreate(a,&i);
BinaryTreeInOrder(root);
}
return 0;
}