mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 08:50:15 +08:00
添加 117.填充每个节点的下一个右侧节点指针II Scala版本
This commit is contained in:
@ -1998,6 +1998,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