Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8030165

Browse files
committed
添加 515.在每个树行中找最大值 Scala版本
1 parent aafc18e commit 8030165

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

‎problems/0102.二叉树的层序遍历.md‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1433,6 +1433,32 @@ func largestValues(_ root: TreeNode?) -> [Int] {
14331433
}
14341434
```
14351435

1436+
Scala:
1437+
```scala
1438+
// 515.在每个树行中找最大值
1439+
object Solution {
1440+
import scala.collection.mutable
1441+
def largestValues(root: TreeNode): List[Int] = {
1442+
val res = mutable.ListBuffer[Int]()
1443+
if (root == null) return res.toList
1444+
val queue = mutable.Queue[TreeNode]()
1445+
queue.enqueue(root)
1446+
while (!queue.isEmpty) {
1447+
var max = Int.MinValue // 初始化max为系统最小值
1448+
val len = queue.size
1449+
for (i <- 0 until len) {
1450+
val curNode = queue.dequeue()
1451+
max = math.max(max, curNode.value) // 对比求解最大值
1452+
if (curNode.left != null) queue.enqueue(curNode.left)
1453+
if (curNode.right != null) queue.enqueue(curNode.right)
1454+
}
1455+
res.append(max) // 将最大值放入结果集
1456+
}
1457+
res.toList
1458+
}
1459+
}
1460+
```
1461+
14361462
# 116.填充每个节点的下一个右侧节点指针
14371463

14381464
[力扣题目链接](https://leetcode-cn.com/problems/populating-next-right-pointers-in-each-node/)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /