Skip to content

Commit 2d15be9

Browse files
committed
feat: add solutions to lc problem: No.0429
No.0429.N-ary Tree Level Order Traversal
1 parent 2aa67b7 commit 2d15be9

File tree

6 files changed

+304
-64
lines changed

6 files changed

+304
-64
lines changed

solution/0400-0499/0429.N-ary Tree Level Order Traversal/README.md

+96-20
Original file line numberDiff line numberDiff line change
@@ -60,22 +60,21 @@ class Node:
6060
self.children = children
6161
"""
6262

63+
6364
class Solution:
6465
def levelOrder(self, root: 'Node') -> List[List[int]]:
66+
ans = []
6567
if root is None:
66-
return []
68+
return ans
6769
q = deque([root])
68-
res = []
6970
while q:
70-
n = len(q)
7171
t = []
72-
for _ in range(n):
73-
node = q.popleft()
74-
t.append(node.val)
75-
if node.children:
76-
q.extend(node.children)
77-
res.append(t)
78-
return res
72+
for _ in range(len(q), 0, -1):
73+
root = q.popleft()
74+
t.append(root.val)
75+
q.extend(root.children)
76+
ans.append(t)
77+
return ans
7978
```
8079

8180
### **Java**
@@ -104,24 +103,22 @@ class Node {
104103

105104
class Solution {
106105
public List<List<Integer>> levelOrder(Node root) {
106+
List<List<Integer>> ans = new ArrayList<>();
107107
if (root == null) {
108-
return Collections.emptyList();
108+
return ans;
109109
}
110110
Deque<Node> q = new ArrayDeque<>();
111-
List<List<Integer>> res = new ArrayList<>();
112111
q.offer(root);
113112
while (!q.isEmpty()) {
114113
List<Integer> t = new ArrayList<>();
115-
for (int i = 0, n = q.size(); i < n; ++i) {
116-
Node node = q.poll();
117-
t.add(node.val);
118-
if (node.children != null) {
119-
q.addAll(node.children);
120-
}
114+
for (int n = q.size(); n > 0; --n) {
115+
root = q.poll();
116+
t.add(root.val);
117+
q.addAll(root.children);
121118
}
122-
res.add(t);
119+
ans.add(t);
123120
}
124-
return res;
121+
return ans;
125122
}
126123
}
127124
```
@@ -151,6 +148,85 @@ class Solution {
151148
}
152149
```
153150

151+
### **C++**
152+
153+
```cpp
154+
/*
155+
// Definition for a Node.
156+
class Node {
157+
public:
158+
int val;
159+
vector<Node*> children;
160+
161+
Node() {}
162+
163+
Node(int _val) {
164+
val = _val;
165+
}
166+
167+
Node(int _val, vector<Node*> _children) {
168+
val = _val;
169+
children = _children;
170+
}
171+
};
172+
*/
173+
174+
class Solution {
175+
public:
176+
vector<vector<int>> levelOrder(Node* root) {
177+
vector<vector<int>> ans;
178+
if (!root) return ans;
179+
queue<Node*> q{{root}};
180+
while (!q.empty())
181+
{
182+
vector<int> t;
183+
for (int n = q.size(); n > 0; --n)
184+
{
185+
root = q.front();
186+
q.pop();
187+
t.push_back(root->val);
188+
for (auto& child : root->children) q.push(child);
189+
}
190+
ans.push_back(t);
191+
}
192+
return ans;
193+
}
194+
};
195+
```
196+
197+
### **Go**
198+
199+
```go
200+
/**
201+
* Definition for a Node.
202+
* type Node struct {
203+
* Val int
204+
* Children []*Node
205+
* }
206+
*/
207+
208+
func levelOrder(root *Node) [][]int {
209+
var ans [][]int
210+
if root == nil {
211+
return ans
212+
}
213+
q := []*Node{root}
214+
for len(q) > 0 {
215+
var t []int
216+
for n := len(q); n > 0; n-- {
217+
root = q[0]
218+
q = q[1:]
219+
t = append(t, root.Val)
220+
for _, child := range root.Children {
221+
q = append(q, child)
222+
}
223+
}
224+
ans = append(ans, t)
225+
}
226+
return ans
227+
}
228+
```
229+
154230
### **...**
155231

156232
```

solution/0400-0499/0429.N-ary Tree Level Order Traversal/README_EN.md

+98-20
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737

3838
## Solutions
3939

40+
BFS or DFS.
41+
4042
<!-- tabs:start -->
4143

4244
### **Python3**
@@ -50,22 +52,21 @@ class Node:
5052
self.children = children
5153
"""
5254

55+
5356
class Solution:
5457
def levelOrder(self, root: 'Node') -> List[List[int]]:
58+
ans = []
5559
if root is None:
56-
return []
60+
return ans
5761
q = deque([root])
58-
res = []
5962
while q:
60-
n = len(q)
6163
t = []
62-
for _ in range(n):
63-
node = q.popleft()
64-
t.append(node.val)
65-
if node.children:
66-
q.extend(node.children)
67-
res.append(t)
68-
return res
64+
for _ in range(len(q), 0, -1):
65+
root = q.popleft()
66+
t.append(root.val)
67+
q.extend(root.children)
68+
ans.append(t)
69+
return ans
6970
```
7071

7172
### **Java**
@@ -92,25 +93,102 @@ class Node {
9293

9394
class Solution {
9495
public List<List<Integer>> levelOrder(Node root) {
96+
List<List<Integer>> ans = new ArrayList<>();
9597
if (root == null) {
96-
return Collections.emptyList();
98+
return ans;
9799
}
98100
Deque<Node> q = new ArrayDeque<>();
99-
List<List<Integer>> res = new ArrayList<>();
100101
q.offer(root);
101102
while (!q.isEmpty()) {
102103
List<Integer> t = new ArrayList<>();
103-
for (int i = 0, n = q.size(); i < n; ++i) {
104-
Node node = q.poll();
105-
t.add(node.val);
106-
if (node.children != null) {
107-
q.addAll(node.children);
108-
}
104+
for (int n = q.size(); n > 0; --n) {
105+
root = q.poll();
106+
t.add(root.val);
107+
q.addAll(root.children);
109108
}
110-
res.add(t);
109+
ans.add(t);
111110
}
112-
return res;
111+
return ans;
112+
}
113+
}
114+
```
115+
116+
### **C++**
117+
118+
```cpp
119+
/*
120+
// Definition for a Node.
121+
class Node {
122+
public:
123+
int val;
124+
vector<Node*> children;
125+
126+
Node() {}
127+
128+
Node(int _val) {
129+
val = _val;
113130
}
131+
132+
Node(int _val, vector<Node*> _children) {
133+
val = _val;
134+
children = _children;
135+
}
136+
};
137+
*/
138+
139+
class Solution {
140+
public:
141+
vector<vector<int>> levelOrder(Node* root) {
142+
vector<vector<int>> ans;
143+
if (!root) return ans;
144+
queue<Node*> q{{root}};
145+
while (!q.empty())
146+
{
147+
vector<int> t;
148+
for (int n = q.size(); n > 0; --n)
149+
{
150+
root = q.front();
151+
q.pop();
152+
t.push_back(root->val);
153+
for (auto& child : root->children) q.push(child);
154+
}
155+
ans.push_back(t);
156+
}
157+
return ans;
158+
}
159+
};
160+
```
161+
162+
### **Go**
163+
164+
```go
165+
/**
166+
* Definition for a Node.
167+
* type Node struct {
168+
* Val int
169+
* Children []*Node
170+
* }
171+
*/
172+
173+
func levelOrder(root *Node) [][]int {
174+
var ans [][]int
175+
if root == nil {
176+
return ans
177+
}
178+
q := []*Node{root}
179+
for len(q) > 0 {
180+
var t []int
181+
for n := len(q); n > 0; n-- {
182+
root = q[0]
183+
q = q[1:]
184+
t = append(t, root.Val)
185+
for _, child := range root.Children {
186+
q = append(q, child)
187+
}
188+
}
189+
ans = append(ans, t)
190+
}
191+
return ans
114192
}
115193
```
116194

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
// Definition for a Node.
3+
class Node {
4+
public:
5+
int val;
6+
vector<Node*> children;
7+
8+
Node() {}
9+
10+
Node(int _val) {
11+
val = _val;
12+
}
13+
14+
Node(int _val, vector<Node*> _children) {
15+
val = _val;
16+
children = _children;
17+
}
18+
};
19+
*/
20+
21+
class Solution {
22+
public:
23+
vector<vector<int>> levelOrder(Node* root) {
24+
vector<vector<int>> ans;
25+
if (!root) return ans;
26+
queue<Node*> q{{root}};
27+
while (!q.empty())
28+
{
29+
vector<int> t;
30+
for (int n = q.size(); n > 0; --n)
31+
{
32+
root = q.front();
33+
q.pop();
34+
t.push_back(root->val);
35+
for (auto& child : root->children) q.push(child);
36+
}
37+
ans.push_back(t);
38+
}
39+
return ans;
40+
}
41+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Definition for a Node.
3+
* type Node struct {
4+
* Val int
5+
* Children []*Node
6+
* }
7+
*/
8+
9+
func levelOrder(root *Node) [][]int {
10+
var ans [][]int
11+
if root == nil {
12+
return ans
13+
}
14+
q := []*Node{root}
15+
for len(q) > 0 {
16+
var t []int
17+
for n := len(q); n > 0; n-- {
18+
root = q[0]
19+
q = q[1:]
20+
t = append(t, root.Val)
21+
for _, child := range root.Children {
22+
q = append(q, child)
23+
}
24+
}
25+
ans = append(ans, t)
26+
}
27+
return ans
28+
}

0 commit comments

Comments
 (0)