-
-
Notifications
You must be signed in to change notification settings - Fork 297
/
Copy path590.cpp
73 lines (66 loc) · 1.63 KB
/
590.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
__________________________________________________________________________________________________
sample 140 ms submission
/*
// Definition for a Node.
class Node {
public:
int val;
vector<Node*> children;
Node() {}
Node(int _val, vector<Node*> _children) {
val = _val;
children = _children;
}
};
*/
const static auto c = []() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
return 0;
}();
class Solution {
public:
vector<int> postorder(Node* root) {
vector<int> postorder;
recursivedfs(root, postorder);
return postorder;
}
void recursivedfs(Node* root, vector<int>& postorder) {
if (!root) return;
for (int i = 0; i < (root -> children).size(); i++) {
recursivedfs((root -> children)[i], postorder);
}
postorder.push_back(root -> val);
}
};
__________________________________________________________________________________________________
sample 32544 kb submission
/*
// Definition for a Node.
class Node {
public:
int val;
vector<Node*> children;
Node() {}
Node(int _val, vector<Node*> _children) {
val = _val;
children = _children;
}
};
*/
class Solution {
public:
vector<int> postorder(Node* root) {
vector<int> res;
postorder(root, res);
return res;
}
void postorder(Node* node, vector<int>& res) {
if (!node) return ;
for (auto n: node->children) {
postorder(n, res);
}
res.push_back(node->val);
}
};
__________________________________________________________________________________________________