-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathuva-10701.cpp
72 lines (57 loc) · 1.48 KB
/
uva-10701.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//uva 10701
//Pre, in and post
#include <iostream>
#include <string>
using namespace std;
typedef struct node{
char value;
node * left;
node * right;
node(){
left = nullptr;
right = nullptr;
}
}node;
node * build_tree(string & preorder, string & inorder, int l_index, int r_index, int & pre_index);
void postorder(node * tree, string & result);
int main(void)
{
int T = 0;
cin >> T;
while(T--){
int n;
cin >> n;
string preorder, inorder;
cin >> preorder >> inorder;
int index = 0;
node * Tree = build_tree(preorder, inorder, 0, n, index);
string result;
postorder(Tree, result);
cout << result << endl;
}
return 0;
}
node * build_tree(string & preorder, string & inorder, int l_index, int r_index, int & pre_index)
{
if(l_index >= r_index)
return nullptr;
node * new_node = new node();
new_node->value = preorder[pre_index];
int loc = l_index;
for(; loc < r_index; ++loc)
if(preorder[pre_index] == inorder[loc])
break;
++pre_index;
new_node->left = build_tree(preorder, inorder, l_index, loc, pre_index);
new_node->right = build_tree(preorder, inorder, loc + 1, r_index, pre_index);
return new_node;
}
void postorder(node * tree, string & result)
{
if(tree == nullptr)
return;
postorder(tree->left, result);
postorder(tree->right, result);
result = result + tree->value;
return;
}