diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index 5afce73a..e707ce27 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -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.二叉树的右视图 [力扣题目链接](https://leetcode-cn.com/problems/binary-tree-right-side-view/)