修改 0107 二叉树层序遍历II JavaScript解法

选择从数组前头插入数值,避免最后反转数组的操作,减少计算时间
This commit is contained in:
Jerry-306
2021-09-25 20:08:41 +08:00
committed by GitHub
parent 0f0bb6e741
commit a2f7147a93

View File

@ -421,9 +421,10 @@ var levelOrderBottom = function(root) {
node.left&&queue.push(node.left); node.left&&queue.push(node.left);
node.right&&queue.push(node.right); node.right&&queue.push(node.right);
} }
res.push(curLevel); // 从数组前头插入值,避免最后反转数组,减少运算时间
res.unshift(curLevel);
} }
return res.reverse(); return res;
}; };
``` ```