Skip to content

Commit 8d37516

Browse files
committed
day 6
1 parent ec412c9 commit 8d37516

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
N-ary Tree Level Order Traversal
3+
================================
4+
5+
Given an n-ary tree, return the level order traversal of its nodes' values.
6+
7+
Nary-Tree input serialization is represented in their level order traversal, each group of children is separated by the null value (See examples).
8+
9+
Example 1:
10+
Input: root = [1,null,3,2,4,null,5,6]
11+
Output: [[1],[3,2,4],[5,6]]
12+
13+
Example 2:
14+
Input: root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]
15+
Output: [[1],[2,3,4,5],[6,7,8,9,10],[11,12,13],[14]]
16+
17+
Constraints:
18+
The height of the n-ary tree is less than or equal to 1000
19+
The total number of nodes is between [0, 104]
20+
*/
21+
22+
/*
23+
// Definition for a Node.
24+
class Node {
25+
public:
26+
int val;
27+
vector<Node*> children;
28+
29+
Node() {}
30+
31+
Node(int _val) {
32+
val = _val;
33+
}
34+
35+
Node(int _val, vector<Node*> _children) {
36+
val = _val;
37+
children = _children;
38+
}
39+
};
40+
*/
41+
42+
class Solution
43+
{
44+
public:
45+
vector<vector<int>> levelOrder(Node *root)
46+
{
47+
vector<vector<int>> ans;
48+
if (!root)
49+
return ans;
50+
51+
queue<Node *> q;
52+
q.push(root);
53+
while (q.size())
54+
{
55+
vector<int> sub;
56+
for (int i = q.size(); i > 0; --i)
57+
{
58+
auto curr = q.front();
59+
q.pop();
60+
sub.push_back(curr->val);
61+
for (auto &child : curr->children)
62+
{
63+
q.push(child);
64+
}
65+
}
66+
ans.push_back(sub);
67+
}
68+
69+
return ans;
70+
}
71+
};

Leetcode Daily Challenge/August-2021/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@
77
| 3. | [Subsets II](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/613/week-1-august-1st-august-7th/3837/) | [cpp](./03.%20Subsets%20II.cpp) |
88
| 4. | [Path Sum II](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/613/week-1-august-1st-august-7th/3838/) | [cpp](./04.%20Path%20Sum%20II.cpp) |
99
| 5. | [Stone Game](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/613/week-1-august-1st-august-7th/3870/) | [cpp](./05.%20Stone%20Game.cpp) |
10+
| 6. | [N-ary Tree Level Order Traversal](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/613/week-1-august-1st-august-7th/3871/) | [cpp](./06.%20N-ary%20Tree%20Level%20Order%20Traversal.cpp) |

0 commit comments

Comments
 (0)