N-ary Tree Level Order Traversal LeetCode solution in JavaScript

1
var levelOrder = function (root) {
2
if (!root) return [];
3

4
const Q = [[root, 0]];
5
const op = [];
6

7
while (Q.length) {
8
const [node, level] = Q.shift();
9

10
if (op.length <= level) {
11
op[level] = [];
12
}
13
op[level].push(node.val);
14

15
for (const child of node.children) {
16
Q.push([child, level + 1]);
17
}
18
}
19
return op;
20
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0