0589.cpp (472B)
1 class Solution { 2 public: 3 vector<int> preorder(Node *root) { 4 if (!root) return {}; 5 vector<int> res; 6 stack<Node *> st; 7 st.push(root); 8 while (!st.empty()) { 9 Node *root = st.top(); 10 st.pop(); 11 res.push_back(root->val); 12 reverse(root->children.begin(), root->children.end()); 13 for (Node *c : root->children) 14 st.push(c); 15 } 16 return res; 17 } 18 };