编写一个程序,读入用户输入的一串先序遍历字符串,根据此字符串建立一个二叉树(以指针方式存储)。
例如如下的先序遍历字符串: abc##de#g##f###
其中 #
表示的是空格,空格字符代表空树。
建立起此二叉树以后,再对二叉树进行中序遍历,输出遍历结果。
输入格式
共一行,包含一个字符串,表示先序遍历字符串。
输出格式
共一行,输出将输入字符串建立二叉树后中序遍历的序列,字符之间用空格隔开。
注意,输出中不用包含 #
。
数据范围
输入字符串长度不超过 100100,且只包含小写字母和 #
。
输入样例:
abc##de#g##f###
输出样例:
c b e g d f a
知识点:二叉树定义、先序、中序遍历
复杂版本思路:创建二叉树结构体,先序读入二叉树数据,然后再中序遍历输出
#include<iostream>
using namespace std;
typedef struct node{
char data;
struct node *lchild,*rchild;
}Node,*bitree;
//创建二叉树
bitree create(){
bitree T;
char ch;
cin >> ch;
if(ch!='#'){
T = new Node;
T->data = ch;
T->lchild = create();
T->rchild = create();
}
else{
T = NULL;
}
return T;
}
//中序遍历
void midOrder(bitree T){
if(T->lchild) midOrder(T->lchild);
cout << T->data << ' ';
if(T->rchild) midOrder(T->rchild);
}
int main(){
bitree T;
T = create();
midOrder(T);
return 0;
}
快捷版本思路:
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
//一边“建树”,一边输出
void build()
{
char ch = getchar();
if(ch == '#')
return ;
build();
cout << ch << ' ';
build();
}
int main()
{
build();
return 0;
}
参考: