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/)