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
|
var levelOrder = function (root) { if (!root) { return []; }
const result = []; const queue = [root];
while (queue.length) { const [group, len] = [[], queue.length];
for (let i = 0; i < len; i++) { const node = queue.shift(); node && (group.push(node.val)); node && queue.push(...node.children); } result.push(group); }
return result; };
|