/*
// 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) {
vector<int >res;
if(!root) return res;
res.push_back(root->val);
for(auto i:root->children)
{
vector<int>tmp=preorder(i);
res.insert(res.end(),tmp.begin(),tmp.end());
}
return res;
}
};
/*
// 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) {
vector<int>res;
if(!root) return res;
stack<Node*>pt;
pt.push(root);
res.push_back(root->val);
while(!pt.empty())
{
if(pt.top()->children.empty())
{
pt.pop();
if(!pt.empty()) pt.top()->children.erase(pt.top()->children.begin());
}
else{
pt.push(pt.top()->children[0]);
res.push_back(pt.top()->val);
}
}
return res;
}
};