N-ary Tree Preorder Traversal LeetCode solution in C++
1class Solution {2public:3vector<int> preorder(Node *root) {4vector<int> ans;5stack<Node *> st;6st.push(root);7while (!st.empty()) {8auto frnt = st.top();9st.pop();10ans.push_back(frnt->val);11for (int i = frnt->children.size() - 1; i >= 0; i--) {12st.push(frnt->children[i]);13}14}15return ans;16}17};