Skip to content

Commit 8d96183

Browse files
authored
429.N-ary Tree Level Order Traversal
429.N-ary Tree Level Order Traversal
1 parent 6f59743 commit 8d96183

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

levelOrder.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> levelOrder(Node* root)
4+
{
5+
if (!root)
6+
return vector<vector<int>>();
7+
vector<vector<int>> res;
8+
queue<Node*> q;
9+
q.push(root);
10+
while (!q.empty())
11+
{
12+
int size = q.size();
13+
vector<int> curLevel;
14+
for (int i = 0; i < size; i++)
15+
{
16+
Node* tmp = q.front();
17+
q.pop();
18+
curLevel.push_back(tmp -> val);
19+
for (Node* n : tmp -> children)
20+
q.push(n);
21+
}
22+
res.push_back(curLevel);
23+
}
24+
return res;
25+
}
26+
};

0 commit comments

Comments
 (0)