-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy path1086_Tree Traversals Again (25).cpp
77 lines (68 loc) · 1.67 KB
/
1086_Tree Traversals Again (25).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
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdio>
#include <cctype>
#include <map>
#include <string>
#include <cstring>
#include <stack>
#include <set>
using namespace std;
class Command {
public:
string op;
int val;
}cmd_ins;
class Node {
public:
Node *left;
Node *right;
int val;
} *root, *nodelist[40];
vector<Command> cmdList;
vector<int> postTravelArr;
stack<int> st;
void postTravel(Node *root) {
if (root->left != NULL)
postTravel(root->left);
if (root->right != NULL)
postTravel(root->right);
postTravelArr.push_back(root->val);
}
int main() {
int n;
cin >> n;
for (int i = 0; i < 2*n; i++) {
cin >> cmd_ins.op;
if (cmd_ins.op == "Push") {
cin >> cmd_ins.val;
st.push(cmd_ins.val);
} else {
cmd_ins.val = st.top();
st.pop();
}
cmdList.push_back(cmd_ins);
nodelist[cmd_ins.val] = new Node;
nodelist[cmd_ins.val]->left = NULL;
nodelist[cmd_ins.val]->right = NULL;
nodelist[cmd_ins.val]->val = cmd_ins.val;
}
root = nodelist[cmdList[0].val];
for (int i = 1; i < cmdList.size(); i++) {
// attach much attention to Push operation
// when two successive Push operation i-1 and i means Node i-1's left child is Node i
// when one operation is Pop, and another operation is Push, so operation i-1 Pop and i Push means, Node i-1's right child is Node i
if (cmdList[i].op == "Push") {
if (cmdList[i-1].op == "Push") nodelist[cmdList[i-1].val]->left = nodelist[cmdList[i].val];
else nodelist[cmdList[i-1].val]->right = nodelist[cmdList[i].val];
}
}
postTravel(root);
cout << postTravelArr[0];
for (int i = 1; i< postTravelArr.size(); i++) {
cout << " " << postTravelArr[i];
}
cout << endl;
return 0;
}