#include #include // Definition for a Node. class Node { public: int val; std::vector children; Node(int _val, std::vector _children) : val(_val), children(std::move(_children)) {} }; /** * 589. N-ary Tree Preorder Traversal * Given the root of an n-ary tree, return the preorder traversal of its nodes' values. * Nary-Tree input serialization is represented in their level order traversal. Each group of children is separated by the null value (See examples) */ class Solution { public: static std::vector preorder(Node* root) { std::vector ret; std::function traverse = [&](const Node* const ptr) { if (!ptr) return; ret.push_back(ptr->val); for (const Node* const i : ptr->children) traverse(i); }; traverse(root); return ret; } };