-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathNaryTreeLevelOrderTraversal429.java
58 lines (52 loc) · 1.32 KB
/
NaryTreeLevelOrderTraversal429.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
* Given an n-ary tree, return the level order traversal of its nodes' values.
* (ie, from left to right, level by level).
*
* For example, given a 3-ary tree:
* https://leetcode.com/static/images/problemset/NaryTreeExample.png
*
* We should return its level order traversal:
*
* [
* [1],
* [3,2,4],
* [5,6]
* ]
*
* Note:
* The depth of the tree is at most 1000.
* The total number of nodes is at most 5000.
*/
/*
// Definition for a Node.
class Node {
public int val;
public List<Node> children;
public Node() {}
public Node(int _val,List<Node> _children) {
val = _val;
children = _children;
}
};
*/
public class NaryTreeLevelOrderTraversal429 {
public List<List<Integer>> levelOrder(Node root) {
List<List<Integer>> res = new ArrayList<>();
if (root == null) return res;
Queue<Node> q = new LinkedList<>();
q.add(root);
while (!q.isEmpty()) {
int size = q.size();
List<Integer> level = new ArrayList<>();
for (int i=0; i<size; i++) {
Node curr = q.poll();
level.add(curr.val);
for (Node n: curr.children) {
q.add(n);
}
}
res.add(level);
}
return res;
}
}