From 04339562c7ce6c46dbd081551e51f36dcaf5fe8d Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Thu, 19 May 2022 10:36:51 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20107.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E5=B1=82=E6=AC=A1=E9=81=8D=E5=8E=86II=20Scal?= =?UTF-8?q?a=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0102.二叉树的层序遍历.md | 27 +++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index 5afce73a..e707ce27 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -553,6 +553,33 @@ func levelOrderBottom(_ root: TreeNode?) -> [[Int]] { } ``` +Scala: +```scala +// 107.二叉树的层次遍历II +object Solution { + import scala.collection.mutable + def levelOrderBottom(root: TreeNode): List[List[Int]] = { + val res = mutable.ListBuffer[List[Int]]() + if (root == null) return res.toList + val queue = mutable.Queue[TreeNode]() + queue.enqueue(root) + while (!queue.isEmpty) { + val tmp = mutable.ListBuffer[Int]() + val len = queue.size + for (i <- 0 until len) { + val curNode = queue.dequeue() + tmp.append(curNode.value) + if (curNode.left != null) queue.enqueue(curNode.left) + if (curNode.right != null) queue.enqueue(curNode.right) + } + res.append(tmp.toList) + } + // 最后翻转一下 + res.reverse.toList + } +} +``` + # 199.二叉树的右视图 [力扣题目链接](https://leetcode-cn.com/problems/binary-tree-right-side-view/)