版权声明:假装有个原创声明……虽然少许博文不属于完全原创,但也是自己辛辛苦苦总结的,转载请注明出处,感谢! https://blog.csdn.net/m0_37454852/article/details/88315948
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
using namespace std;
typedef struct BiTNode
{
char data;
struct BiTNode *lChild, *rChild;
}BiTNode, *BiTree;
char str[110];
int cnt = 0, len = 0;
BiTree buildTree()//根据输入的字符串建树;根据先序遍历思想进行
{
if(str[cnt] == '#') return NULL;
BiTree T = new BiTNode;
T->data = str[cnt];
cnt++;
T->lChild = buildTree();
cnt++;
T->rChild = buildTree();
return T;
}
void inOrder(BiTree T)//中序遍历,打印
{
if(!T) return ;
inOrder(T->lChild);
printf("%c ", T->data);//题目要求每个字符后都要有空格,注意
inOrder(T->rChild);
}
int main()
{
while(scanf("%s", str) != EOF)
{
cnt = 0, len = strlen(str);
BiTree ROOT = buildTree();
inOrder(ROOT);
printf("\n");//输出回车换行
}
return 0;
}