-
Notifications
You must be signed in to change notification settings - Fork 0
/
1194.cpp
54 lines (42 loc) · 1019 Bytes
/
1194.cpp
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
45
46
47
48
49
50
51
52
53
54
#include <bits/stdc++.h>
using namespace std;
struct node {
char data;
node *left, *right;
node(char data) : data(data), left(NULL), right(NULL) {}
};
node *build(string pre, string in)
{
if(pre.empty() || in.empty()) return NULL;
node *root = new node(pre[0]);
int root_pos = in.find(pre[0]);
string left_in = in.substr(0, root_pos);
string right_in = in.substr(root_pos+1);
int left_size = left_in.size();
string left_pre = pre.substr(1, left_size);
string right_pre = pre.substr(left_size+1);
root->left = build(left_pre, left_in);
root->right = build(right_pre, right_in);
return root;
}
void postorder(node *root)
{
if(root == NULL) return;
postorder(root->left);
postorder(root->right);
cout << root->data;
}
int main()
{
int c;
cin >> c;
while(c--)
{
int n;
string pre, in;
cin >> n >> pre >> in;
node *root = build(pre, in);
postorder(root);
cout << endl;
}
}