mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 02:53:31 +08:00
添加 515.在每个树行中找最大值 Scala版本
This commit is contained in:
@ -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/)
|
||||||
|
Reference in New Issue
Block a user