添加 515.在每个树行中找最大值 Scala版本

This commit is contained in:
ZongqinWang
2022-05-19 12:29:12 +08:00
parent aafc18ee12
commit 80301657b3

View File

@ -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.填充每个节点的下一个右侧节点指针 # 116.填充每个节点的下一个右侧节点指针
[力扣题目链接](https://leetcode-cn.com/problems/populating-next-right-pointers-in-each-node/) [力扣题目链接](https://leetcode-cn.com/problems/populating-next-right-pointers-in-each-node/)