-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path589.cpp
More file actions
34 lines (31 loc) · 721 Bytes
/
589.cpp
File metadata and controls
34 lines (31 loc) · 721 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
/*
// 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> preorder(Node* root) {
stack<Node*> myStack;
myStack.push(root);
vector<int> result;
if(root==NULL) return {};
while(myStack.size() != 0){
Node* top = myStack.top();
result.push_back(top->val);
myStack.pop();
for(int i = top->children.size() - 1; i >= 0; i--){
myStack.push(top->children[i]);
}
}
return result;
}
};