1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-11-10 00:09:06 +01:00
LeetCode/problems/n-ary-tree-preorder-traversal.cpp
Matej Focko b59f949d8f
problems: add n-ary tree preorder traversal
Signed-off-by: Matej Focko <mfocko@redhat.com>
2022-07-23 13:11:02 +02:00

43 lines
696 B
C++

/*
// 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 {
void preorder(vector<int>& traversal, Node* root)
{
if (root == nullptr) {
return;
}
traversal.push_back(root->val);
for (auto child : root->children) {
preorder(traversal, child);
}
}
public:
vector<int> preorder(Node* root)
{
vector<int> result;
preorder(result, root);
return result;
}
};