From 821ca656e14384fd63e937a0cd99fbe7bde813f0 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Fri, 27 May 2022 16:10:35 +0800
Subject: [PATCH 1/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200654.=E6=9C=80?=
=?UTF-8?q?=E5=A4=A7=E4=BA=8C=E5=8F=89=E6=A0=91.md=20Scala=E7=89=88?=
=?UTF-8?q?=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0654.最大二叉树.md | 26 ++++++++++++++++++++++++++
1 file changed, 26 insertions(+)
diff --git a/problems/0654.最大二叉树.md b/problems/0654.最大二叉树.md
index 1c73354b..1b0eac66 100644
--- a/problems/0654.最大二叉树.md
+++ b/problems/0654.最大二叉树.md
@@ -476,7 +476,33 @@ func traversal(_ nums: inout [Int], _ left: Int, _ right: Int) -> TreeNode? {
}
```
+## Scala
+```scala
+object Solution {
+ def constructMaximumBinaryTree(nums: Array[Int]): TreeNode = {
+ if (nums.size == 0) return null
+ // 找到数组最大值
+ var maxIndex = 0
+ var maxValue = Int.MinValue
+ for (i <- nums.indices) {
+ if (nums(i) > maxValue) {
+ maxIndex = i
+ maxValue = nums(i)
+ }
+ }
+
+ // 构建一棵树
+ var root = new TreeNode(maxValue, null, null)
+
+ // 递归寻找左右子树
+ root.left = constructMaximumBinaryTree(nums.slice(0, maxIndex))
+ root.right = constructMaximumBinaryTree(nums.slice(maxIndex + 1, nums.length))
+
+ root // 返回root
+ }
+}
+```
-----------------------
From de34170f5cc1526eb0c4b1860943a57e6f41c4e1 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Fri, 27 May 2022 16:37:53 +0800
Subject: [PATCH 2/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200617.=E5=90=88?=
=?UTF-8?q?=E5=B9=B6=E4=BA=8C=E5=8F=89=E6=A0=91.md=20Scala=E7=89=88?=
=?UTF-8?q?=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0617.合并二叉树.md | 53 ++++++++++++++++++++++++++++++++
1 file changed, 53 insertions(+)
diff --git a/problems/0617.合并二叉树.md b/problems/0617.合并二叉树.md
index 55786ea9..d8bdc91c 100644
--- a/problems/0617.合并二叉树.md
+++ b/problems/0617.合并二叉树.md
@@ -631,7 +631,60 @@ function mergeTrees(root1: TreeNode | null, root2: TreeNode | null): TreeNode |
};
```
+## Scala
+递归:
+```scala
+object Solution {
+ def mergeTrees(root1: TreeNode, root2: TreeNode): TreeNode = {
+ if (root1 == null) return root2 // 如果root1为空,返回root2
+ if (root2 == null) return root1 // 如果root2为空,返回root1
+ // 新建一个节点,值为两个节点的和
+ var node = new TreeNode(root1.value + root2.value)
+ // 往下递归
+ node.left = mergeTrees(root1.left, root2.left)
+ node.right = mergeTrees(root1.right, root2.right)
+ node // 返回node,return关键字可以省略
+ }
+}
+```
+
+迭代:
+```scala
+object Solution {
+ import scala.collection.mutable
+ def mergeTrees(root1: TreeNode, root2: TreeNode): TreeNode = {
+ if (root1 == null) return root2
+ if (root2 == null) return root1
+ var stack = mutable.Stack[TreeNode]()
+ // 先放node2再放node1
+ stack.push(root2)
+ stack.push(root1)
+ while (!stack.isEmpty) {
+ var node1 = stack.pop()
+ var node2 = stack.pop()
+ node1.value += node2.value
+ if (node1.right != null && node2.right != null) {
+ stack.push(node2.right)
+ stack.push(node1.right)
+ } else {
+ if(node1.right == null){
+ node1.right = node2.right
+ }
+ }
+ if (node1.left != null && node2.left != null) {
+ stack.push(node2.left)
+ stack.push(node1.left)
+ } else {
+ if(node1.left == null){
+ node1.left = node2.left
+ }
+ }
+ }
+ root1
+ }
+}
+```
-----------------------