From 80301657b305bcbd614d5294b1d483d31419755f Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Thu, 19 May 2022 12:29:12 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20515.=E5=9C=A8=E6=AF=8F?= =?UTF-8?q?=E4=B8=AA=E6=A0=91=E8=A1=8C=E4=B8=AD=E6=89=BE=E6=9C=80=E5=A4=A7?= =?UTF-8?q?=E5=80=BC=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 | 26 +++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index ab8f2e57..db27c73d 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -1433,6 +1433,32 @@ func largestValues(_ root: TreeNode?) -> [Int] { } ``` +Scala: +```scala +// 515.在每个树行中找最大值 +object Solution { + import scala.collection.mutable + def largestValues(root: TreeNode): List[Int] = { + val res = mutable.ListBuffer[Int]() + if (root == null) return res.toList + val queue = mutable.Queue[TreeNode]() + queue.enqueue(root) + while (!queue.isEmpty) { + var max = Int.MinValue // 初始化max为系统最小值 + val len = queue.size + for (i <- 0 until len) { + val curNode = queue.dequeue() + max = math.max(max, curNode.value) // 对比求解最大值 + if (curNode.left != null) queue.enqueue(curNode.left) + if (curNode.right != null) queue.enqueue(curNode.right) + } + res.append(max) // 将最大值放入结果集 + } + res.toList + } +} +``` + # 116.填充每个节点的下一个右侧节点指针 [力扣题目链接](https://leetcode-cn.com/problems/populating-next-right-pointers-in-each-node/) From a3179499d03b05594b8808a1d11a0ab22048cf32 Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Thu, 19 May 2022 12:58:52 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20116.=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?=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 db27c73d..18fdfcb3 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -1718,6 +1718,35 @@ func connect(_ root: Node?) -> Node? { } ``` +Scala: +```scala +// 116.填充每个节点的下一个右侧节点指针 +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 + } +} +``` # 117.填充每个节点的下一个右侧节点指针II [力扣题目链接](https://leetcode-cn.com/problems/populating-next-right-pointers-in-each-node-ii/) 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 3/3] =?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/)