N-ary Tree Preorder Traversal LeetCode solution in C++

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

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0