-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path590.cpp
More file actions
35 lines (32 loc) · 764 Bytes
/
Copy path590.cpp
File metadata and controls
35 lines (32 loc) · 764 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
/*
// 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) {
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 = 0; i < top->children.size(); i++){
myStack.push(top->children[i]);
}
}
reverse(result.begin(), result.end());
return result;
}
};