From 648014b28a54e953d335e822d7e7a5f98a82e3ea Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Thu, 19 May 2022 10:19:22 +0800
Subject: [PATCH 1/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E4=BA=8C=E5=8F=89?=
=?UTF-8?q?=E6=A0=91=E7=9A=84=E7=BB=9F=E4=B8=80=E8=BF=AD=E4=BB=A3=E6=B3=95?=
=?UTF-8?q?.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/二叉树的统一迭代法.md | 74 +++++++++++++++++++++++++
1 file changed, 74 insertions(+)
diff --git a/problems/二叉树的统一迭代法.md b/problems/二叉树的统一迭代法.md
index f6edf586..9ca6ac39 100644
--- a/problems/二叉树的统一迭代法.md
+++ b/problems/二叉树的统一迭代法.md
@@ -591,6 +591,80 @@ function postorderTraversal(root: TreeNode | null): number[] {
return res;
};
```
+Scala:
+```scala
+// 前序遍历
+object Solution {
+ import scala.collection.mutable
+ def preorderTraversal(root: TreeNode): List[Int] = {
+ val res = mutable.ListBuffer[Int]()
+ val stack = mutable.Stack[TreeNode]()
+ if (root != null) stack.push(root)
+ while (!stack.isEmpty) {
+ var curNode = stack.top
+ if (curNode != null) {
+ stack.pop()
+ if (curNode.right != null) stack.push(curNode.right)
+ if (curNode.left != null) stack.push(curNode.left)
+ stack.push(curNode)
+ stack.push(null)
+ } else {
+ stack.pop()
+ res.append(stack.pop().value)
+ }
+ }
+ res.toList
+ }
+}
+// 中序遍历
+object Solution {
+ import scala.collection.mutable
+ def inorderTraversal(root: TreeNode): List[Int] = {
+ val res = mutable.ListBuffer[Int]()
+ val stack = mutable.Stack[TreeNode]()
+ if (root != null) stack.push(root)
+ while (!stack.isEmpty) {
+ var curNode = stack.top
+ if (curNode != null) {
+ stack.pop()
+ if (curNode.right != null) stack.push(curNode.right)
+ stack.push(curNode)
+ stack.push(null)
+ if (curNode.left != null) stack.push(curNode.left)
+ } else {
+ // 等于空的时候好办,弹出这个元素
+ stack.pop()
+ res.append(stack.pop().value)
+ }
+ }
+ res.toList
+ }
+}
+
+// 后序遍历
+object Solution {
+ import scala.collection.mutable
+ def postorderTraversal(root: TreeNode): List[Int] = {
+ val res = mutable.ListBuffer[Int]()
+ val stack = mutable.Stack[TreeNode]()
+ if (root != null) stack.push(root)
+ while (!stack.isEmpty) {
+ var curNode = stack.top
+ if (curNode != null) {
+ stack.pop()
+ stack.push(curNode)
+ stack.push(null)
+ if (curNode.right != null) stack.push(curNode.right)
+ if (curNode.left != null) stack.push(curNode.left)
+ } else {
+ stack.pop()
+ res.append(stack.pop().value)
+ }
+ }
+ res.toList
+ }
+}
+```
-----------------------
From 33ed3d980eecec6824fb5b9edad83d7b916c4142 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Thu, 19 May 2022 10:35:55 +0800
Subject: [PATCH 2/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20102.=E4=BA=8C=E5=8F=89?=
=?UTF-8?q?=E6=A0=91=E7=9A=84=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86=20Scala?=
=?UTF-8?q?=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0102.二叉树的层序遍历.md | 25 +++++++++++++++++++++++
1 file changed, 25 insertions(+)
diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md
index ab8f2e57..5afce73a 100644
--- a/problems/0102.二叉树的层序遍历.md
+++ b/problems/0102.二叉树的层序遍历.md
@@ -298,6 +298,31 @@ func levelOrder(_ root: TreeNode?) -> [[Int]] {
return result
}
```
+Scala:
+```scala
+// 102.二叉树的层序遍历
+object Solution {
+ import scala.collection.mutable
+ def levelOrder(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) // 把根节点加入queue
+ while (!queue.isEmpty) {
+ val tmp = mutable.ListBuffer[Int]()
+ val len = queue.size // 求出len的长度
+ for (i <- 0 until len) { // 从0到当前队列长度的所有节点都加入到结果集
+ 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.toList
+ }
+}
+```
**此时我们就掌握了二叉树的层序遍历了,那么如下九道力扣上的题目,只需要修改模板的两三行代码(不能再多了),便可打倒!**
From 04339562c7ce6c46dbd081551e51f36dcaf5fe8d Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Thu, 19 May 2022 10:36:51 +0800
Subject: [PATCH 3/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20107.=E4=BA=8C=E5=8F=89?=
=?UTF-8?q?=E6=A0=91=E7=9A=84=E5=B1=82=E6=AC=A1=E9=81=8D=E5=8E=86II=20Scal?=
=?UTF-8?q?a=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0102.二叉树的层序遍历.md | 27 +++++++++++++++++++++++
1 file changed, 27 insertions(+)
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/)