-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathuva-11234.cpp
83 lines (68 loc) · 1.31 KB
/
uva-11234.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
73
74
75
76
77
78
79
80
81
82
83
//uva 11234
//Expressions
#include <iostream>
#include <string>
#include <stack>
#include <queue>
using namespace std;
typedef struct node{
char value;
node * left;
node * right;
node(){
left = right = nullptr;
}
}node;
node * build_tree(string input);
void delete_tree(node * Tree);
int main(void)
{
int T;
cin >> T;
while(T--){
string postfix;
cin >> postfix;
node * Tree = build_tree(postfix);
string result;
queue <node *> Queue;
Queue.push(Tree);
while(!Queue.empty()){
node * front = Queue.front();
Queue.pop();
result = front->value + result;
if(front->right != nullptr)
Queue.push(front->right);
if(front->left != nullptr)
Queue.push(front->left);
}
cout << result << endl;
delete_tree(Tree);
}
return 0;
}
node * build_tree(string input)
{
stack <node *> Stack;
for(int start = 0; start < (int)input.size(); ++start){
node * new_node = new node();
new_node->value = input[start];
if(input[start] >= 'A' && input[start] <= 'Z'){
node * op1 = Stack.top();
Stack.pop();
node * op2 = Stack.top();
Stack.pop();
new_node->left = op1;
new_node->right = op2;
}
Stack.push(new_node);
}
return Stack.top();
}
void delete_tree(node * Tree)
{
if(Tree == nullptr)
return;
delete_tree(Tree->left);
delete_tree(Tree->right);
delete Tree;
}