-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy path0589.cpp
More file actions
39 lines (36 loc) · 664 Bytes
/
0589.cpp
File metadata and controls
39 lines (36 loc) · 664 Bytes
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
/*
// Definition for a Node.
class Node {
public:
int val;
vector<Node*> children;
Node() {}
Node(int _val) {
val = _val;
}
Node(int _val, vector<Node*> _children) {
val = _val;
children = _children;
}
};
*/
class Solution {
public:
vector<int> preorder(Node* root) {
vector<int> res;
if (!root) {
return res;
}
stack<Node*> s;
s.emplace(root);
while (!s.empty()) {
Node* t = s.top();
res.emplace_back(t->val);
s.pop();
for (auto it = t->children.rbegin(); it != t->children.rend(); ++it) {
s.emplace(*it);
}
}
return res;
}
};