From 45a01eed32598a6cf4c9a20d995aa46efe296dd5 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Sun, 29 May 2022 13:04:37 +0800
Subject: [PATCH 1/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200701.=E4=BA=8C?=
=?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84=E6=8F=92?=
=?UTF-8?q?=E5=85=A5=E6=93=8D=E4=BD=9C.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../0701.二叉搜索树中的插入操作.md | 37 +++++++++++++++++++
1 file changed, 37 insertions(+)
diff --git a/problems/0701.二叉搜索树中的插入操作.md b/problems/0701.二叉搜索树中的插入操作.md
index 50e39ade..135911b9 100644
--- a/problems/0701.二叉搜索树中的插入操作.md
+++ b/problems/0701.二叉搜索树中的插入操作.md
@@ -585,6 +585,43 @@ function insertIntoBST(root: TreeNode | null, val: number): TreeNode | null {
```
+## Scala
+
+递归:
+
+```scala
+object Solution {
+ def insertIntoBST(root: TreeNode, `val`: Int): TreeNode = {
+ if (root == null) return new TreeNode(`val`)
+ if (`val` < root.value) root.left = insertIntoBST(root.left, `val`)
+ else root.right = insertIntoBST(root.right, `val`)
+ root // 返回根节点
+ }
+}
+```
+
+迭代:
+
+```scala
+object Solution {
+ def insertIntoBST(root: TreeNode, `val`: Int): TreeNode = {
+ if (root == null) {
+ return new TreeNode(`val`)
+ }
+ var parent = root // 记录当前节点的父节点
+ var curNode = root
+ while (curNode != null) {
+ parent = curNode
+ if(`val` < curNode.value) curNode = curNode.left
+ else curNode = curNode.right
+ }
+ if(`val` < parent.value) parent.left = new TreeNode(`val`)
+ else parent.right = new TreeNode(`val`)
+ root // 最终返回根节点
+ }
+}
+```
+
-----------------------
From 8eb956b553b178d3d14555c270f8b726baea5870 Mon Sep 17 00:00:00 2001
From: ZongqinWang <1722249371@qq.com>
Date: Mon, 30 May 2022 10:29:31 +0800
Subject: [PATCH 2/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200450.=E5=88=A0?=
=?UTF-8?q?=E9=99=A4=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E4=B8=AD?=
=?UTF-8?q?=E7=9A=84=E8=8A=82=E7=82=B9.md=20Scala=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../0450.删除二叉搜索树中的节点.md | 28 +++++++++++++++++++
1 file changed, 28 insertions(+)
diff --git a/problems/0450.删除二叉搜索树中的节点.md b/problems/0450.删除二叉搜索树中的节点.md
index e8f7e54c..8367a145 100644
--- a/problems/0450.删除二叉搜索树中的节点.md
+++ b/problems/0450.删除二叉搜索树中的节点.md
@@ -582,7 +582,35 @@ function deleteNode(root: TreeNode | null, key: number): TreeNode | null {
};
```
+## Scala
+```scala
+object Solution {
+ def deleteNode(root: TreeNode, key: Int): TreeNode = {
+ if (root == null) return root // 第一种情况,没找到删除的节点,遍历到空节点直接返回
+ if (root.value == key) {
+ // 第二种情况: 左右孩子都为空,直接删除节点,返回null
+ if (root.left == null && root.right == null) return null
+ // 第三种情况: 左孩子为空,右孩子不为空,右孩子补位
+ else if (root.left == null && root.right != null) return root.right
+ // 第四种情况: 左孩子不为空,右孩子为空,左孩子补位
+ else if (root.left != null && root.right == null) return root.left
+ // 第五种情况: 左右孩子都不为空,将删除节点的左子树头节点(左孩子)放到
+ // 右子树的最左边节点的左孩子上,返回删除节点的右孩子
+ else {
+ var tmp = root.right
+ while (tmp.left != null) tmp = tmp.left
+ tmp.left = root.left
+ return root.right
+ }
+ }
+ if (root.value > key) root.left = deleteNode(root.left, key)
+ if (root.value < key) root.right = deleteNode(root.right, key)
+
+ root // 返回根节点,return关键字可以省略
+ }
+}
+```
-----------------------