From 7a62a75e399325cf439eebf507e8ce2d576e618d Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Thu, 19 May 2022 12:59:45 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20117.=E5=A1=AB=E5=85=85?= =?UTF-8?q?=E6=AF=8F=E4=B8=AA=E8=8A=82=E7=82=B9=E7=9A=84=E4=B8=8B=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E5=8F=B3=E4=BE=A7=E8=8A=82=E7=82=B9=E6=8C=87=E9=92=88?= =?UTF-8?q?II=20Scala=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0102.二叉树的层序遍历.md | 29 +++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index 18fdfcb3..a2130f7e 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -1998,6 +1998,35 @@ func connect(_ root: Node?) -> Node? { } ``` +Scala: +```scala +// 117.填充每个节点的下一个右侧节点指针II +object Solution { + import scala.collection.mutable + + def connect(root: Node): Node = { + if (root == null) return root + val queue = mutable.Queue[Node]() + queue.enqueue(root) + while (!queue.isEmpty) { + val len = queue.size + val tmp = mutable.ListBuffer[Node]() + for (i <- 0 until len) { + val curNode = queue.dequeue() + tmp.append(curNode) + if (curNode.left != null) queue.enqueue(curNode.left) + if (curNode.right != null) queue.enqueue(curNode.right) + } + // 处理next指针 + for (i <- 0 until tmp.size - 1) { + tmp(i).next = tmp(i + 1) + } + tmp(tmp.size-1).next = null + } + root + } +} +``` # 104.二叉树的最大深度 [力扣题目链接](https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/)