mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
Merge pull request #1353 from wzqwtt/tree04
添加(515.在每个树行中找最大值、116.填充每个节点的下一个右侧节点指针、117.填充每个节点的下一个右侧节点指针II)Scala版本
This commit is contained in:
@ -1640,6 +1640,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/)
|
||||
@ -1899,6 +1925,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/)
|
||||
@ -2150,6 +2205,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/)
|
||||
|
Reference in New Issue
Block a user