From a2f7147a9386ca8949cc4774c0ea6e57468119b3 Mon Sep 17 00:00:00 2001 From: Jerry-306 <82520819+Jerry-306@users.noreply.github.com> Date: Sat, 25 Sep 2021 20:08:41 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=200107=20=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86II=20JavaScript?= =?UTF-8?q?=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 选择从数组前头插入数值,避免最后反转数组的操作,减少计算时间 --- problems/0102.二叉树的层序遍历.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index 5128d6ec..7c43a472 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -421,9 +421,10 @@ var levelOrderBottom = function(root) { node.left&&queue.push(node.left); node.right&&queue.push(node.right); } - res.push(curLevel); + // 从数组前头插入值,避免最后反转数组,减少运算时间 + res.unshift(curLevel); } - return res.reverse(); + return res; }; ```