-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathNAryLevelOrderTraversal.java
42 lines (32 loc) · 1.16 KB
/
NAryLevelOrderTraversal.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*https://leetcode.com/problems/n-ary-tree-level-order-traversal/*/
class Solution {
public List<List<Integer>> levelOrder(Node root) {
//edge case
if (root == null) return new ArrayList<List<Integer>>();
List<List<Integer>> list = new ArrayList<List<Integer>>();
Queue<Node> queue = new LinkedList<>();
//add root to queue
queue.add(root);
//till queue is not empty
while (!queue.isEmpty())
{
//find the current level size
int len = queue.size();
List<Integer> temp = new ArrayList<Integer>();
//for every element
for (int i = 0; i < len; ++i)
{
//remove from queue
Node node = queue.remove();
//add all its children to queue
for (int j = 0; j < node.children.size(); ++j)
queue.add(node.children.get(j));
//add removed node to temporary list
temp.add(node.val);
}
//add temporary list to result
list.add(temp);
}
return list;
}
}