AcWing 1256. 扩展二叉树
宋标 Lv5

题目

由于先序、中序和后序序列中的任一个都不能唯一确定一棵二叉树,所以对二叉树做如下处理,将二叉树的空结点用·补齐,如图所示。

我们把这样处理后的二叉树称为原二叉树的扩展二叉树,扩展二叉树的先序和后序序列能唯一确定其二叉树。

2.png

现给出扩展二叉树的先序序列,要求输出原二叉树中序和后序序列。

输入格式

扩展二叉树的先序序列。

输出格式

输出其中序和后序序列。

数据范围

原二叉树的结点数不超过26,且均由大写字母表示。

输入样例:

ABD..EF..G..C..

输出样例:

DBFEGAC
DFGEBCA

题解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

using namespace std;

string in, post;

void dfs()
{
char t;
if (cin >> t, t == '.') return;
dfs();
in += t;
dfs();
post += t;
}

int main()
{
dfs();
cout << in << endl << post << endl;
}
 评论