Tree Recovery(二叉树的DFS)

描述

传送门:UVa536

Little Valentine liked playing with binary trees very much. Her favorite game was constructing randomly looking binary trees with capital letters in the nodes.
This is an example of one of her creations:

          D
         / \
        /   \
       B     E
      / \     \
     /   \     \
    A     C     G
               /
              /
             F

To record her trees for future generations, she wrote down two strings for each tree: a preorder traversal (root, left subtree, right subtree) and an inorder traversal (left subtree, root, right subtree). For the tree drawn above the preorder traversal is DBACEGF and the inorder traversal is ABCDEFG.
She thought that such a pair of strings would give enough information to reconstruct the tree later (but she never tried it).

Now, years later, looking again at the strings, she realized that reconstructing the trees was indeed possible, but only because she never had used the same letter twice in the same tree.
However, doing the reconstruction by hand, soon turned out to be tedious.
So now she asks you to write a program that does the job for her!

Input

The input will contain one or more test cases.
Each test case consists of one line containing two strings preord and inord, representing the preorder traversal and inorder traversal of a binary tree. Both strings consist of unique capital letters. (Thus they are not longer than 26 characters.)
Input is terminated by end of file.

Output

For each test case, recover Valentine’s binary tree and print one line containing the tree’s postorder traversal (left subtree, right subtree, root).

Sample Input

1
2
DBACEGF ABCDEFG
BCAD CBAD

Sample Output

1
2
ACBFGED
CDAB

思路

  • 题意:已知二叉树的先序遍历和中序遍历,求后序遍历。
  • 从中序遍历中找先序遍历的根结点,然后向两边递归,并在最后输出这个结点。
  • 主要是理解二叉树的递归遍历(DFS):
    • PreOrder(T) = T的根结点 + PreOrder(T的左子树) + PreOrder(T的右子树)
    • InOrder(T) = InOrder(T的左子树) + T的根结点 + InOrder(T的右子树)
    • PostOrder(T) = PostOrder(T的左子树) + PostOrder(T的右子树) + T的根结点

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <bits/stdc++.h>
using namespace std;
#define clr(a,x) memset(a, x, sizeof(a))
#define mp(x,y) make_pair(x,y)
#define pb(x) push_back(x)
#define X first
#define Y second
#define fastin ios_base::sync_with_stdio(0);cin.tie(0);
typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef vector<int> VI;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-6;

const int maxn = 30;
char preo[maxn];
char ino[maxn];

void dfs(int s1, int t1, int s2, int t2)
{
int rt;
if (s1 > t1) return; // 子树为空结束递归
for (rt = s2; ino[rt] != preo[s1]; rt++); // 在中序遍历中找到根节点
dfs(s1 + 1, s1 + rt - s2, s2, rt - 1); // 递归左子树
dfs(s1 + rt - s2 + 1, t1, rt + 1, t2); // 递归右子树
cout << ino[rt];
}

int main()
{
#ifndef ONLINE_JUDGE
freopen("1.in", "r", stdin);
freopen("1.out", "w", stdout);
#endif
while (cin >> preo >> ino)
{
int l = strlen(preo) - 1;
dfs(0, l, 0, l);
cout << endl;
}
return 0;
}
捐助作者