From 4417f180dd0a77ce850ec236a2b430d8e185870b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BA=AA=E9=A3=9E?= Date: Tue, 18 May 2021 12:35:01 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E2=80=9C102.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86?= =?UTF-8?q?=E2=80=9DJavascript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0102.二叉树的层序遍历.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index 0ab67b13..1cb4164f 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -687,6 +687,26 @@ func levelOrder(root *TreeNode) [][]int { return result } ``` +Javascript: +```javascript +var levelOrder = function (root) { + let ans = []; + if (!root) return ans; + let queue = [root]; + while (queue.length) { + let size = queue.length; + let temp = []; + while (size--) { + let n = queue.shift(); + temp.push(n.val); + if (n.left) queue.push(n.left); + if (n.right) queue.push(n.right); + } + ans.push(temp); + } + return ans; +}; +``` ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)