-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03_3.cpp
More file actions
60 lines (58 loc) · 1.31 KB
/
Copy path03_3.cpp
File metadata and controls
60 lines (58 loc) · 1.31 KB
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
#include <stack>
#include <cstdio>
#include <vector>
#include <iostream>
using namespace std;
vector<int> Tree[30];
vector<int> reslut;
/*±éÀú*/
void postOrderTraversal(int fNode)
{
if (Tree[fNode].size() == 1) {
postOrderTraversal(Tree[fNode][0]); //ºóÐò±éÀúµÄÌØµã
reslut.push_back(fNode);
}
else if (Tree[fNode].size() == 2) {
postOrderTraversal(Tree[fNode][0]);
postOrderTraversal(Tree[fNode][1]);
reslut.push_back(fNode);
}
else {
reslut.push_back(fNode);
}
}
int main()
{
int n, i, val,
root = 0,
popVal = -1;
stack<int> stk;
cin >> n;
string action;
cin >> action;
cin >> root;
stk.push(root);
for (i = 1; i < 2 * n; ++i) {
cin >> action;
if (action == "Push") {
scanf("%d", &val);
if (popVal == -1) {
Tree[stk.top()].push_back(val);
}
else {
Tree[popVal].push_back(val);
}
stk.push(val);
popVal = -1;
}
else if (action == "Pop") {
popVal = stk.top();
stk.pop();
}
}
postOrderTraversal(root);
for (i = 0; i < reslut.size(); ++i) {
cout << reslut[i] << ((i == reslut.size()-1) ? '\n' : ' ');
}
return 0;
}