mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
添加 107.二叉树的层次遍历II Scala版本
This commit is contained in:
@ -553,6 +553,33 @@ func levelOrderBottom(_ root: TreeNode?) -> [[Int]] {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Scala:
|
||||||
|
```scala
|
||||||
|
// 107.二叉树的层次遍历II
|
||||||
|
object Solution {
|
||||||
|
import scala.collection.mutable
|
||||||
|
def levelOrderBottom(root: TreeNode): List[List[Int]] = {
|
||||||
|
val res = mutable.ListBuffer[List[Int]]()
|
||||||
|
if (root == null) return res.toList
|
||||||
|
val queue = mutable.Queue[TreeNode]()
|
||||||
|
queue.enqueue(root)
|
||||||
|
while (!queue.isEmpty) {
|
||||||
|
val tmp = mutable.ListBuffer[Int]()
|
||||||
|
val len = queue.size
|
||||||
|
for (i <- 0 until len) {
|
||||||
|
val curNode = queue.dequeue()
|
||||||
|
tmp.append(curNode.value)
|
||||||
|
if (curNode.left != null) queue.enqueue(curNode.left)
|
||||||
|
if (curNode.right != null) queue.enqueue(curNode.right)
|
||||||
|
}
|
||||||
|
res.append(tmp.toList)
|
||||||
|
}
|
||||||
|
// 最后翻转一下
|
||||||
|
res.reverse.toList
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
# 199.二叉树的右视图
|
# 199.二叉树的右视图
|
||||||
|
|
||||||
[力扣题目链接](https://leetcode-cn.com/problems/binary-tree-right-side-view/)
|
[力扣题目链接](https://leetcode-cn.com/problems/binary-tree-right-side-view/)
|
||||||
|
Reference in New Issue
Block a user