From f035549e04458aa52b8d4c6843a7b6938a0cd9a6 Mon Sep 17 00:00:00 2001
From: jojoo15 <75017412+jojoo15@users.noreply.github.com>
Date: Wed, 19 May 2021 15:40:05 +0200
Subject: [PATCH 01/12] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200501.=E4=BA=8C?=
=?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84=E4=BC=97?=
=?UTF-8?q?=E6=95=B0=20python=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0501.二叉搜索树中的众数.md | 37 ++++++++++++++++++--
1 file changed, 34 insertions(+), 3 deletions(-)
diff --git a/problems/0501.二叉搜索树中的众数.md b/problems/0501.二叉搜索树中的众数.md
index 385ce2f1..0ddd8b0c 100644
--- a/problems/0501.二叉搜索树中的众数.md
+++ b/problems/0501.二叉搜索树中的众数.md
@@ -394,8 +394,39 @@ class Solution {
```
Python:
-
-
+```python
+# Definition for a binary tree node.
+# class TreeNode:
+# def __init__(self, val=0, left=None, right=None):
+# self.val = val
+# self.left = left
+# self.right = right
+//递归法
+class Solution:
+ def findMode(self, root: TreeNode) -> List[int]:
+ if not root: return
+ self.pre = root
+ self.count = 0 //统计频率
+ self.countMax = 0 //最大频率
+ self.res = []
+ def findNumber(root):
+ if not root: return None // 第一个节点
+ findNumber(root.left) //左
+ if self.pre.val == root.val: //中: 与前一个节点数值相同
+ self.count += 1
+ else: // 与前一个节点数值不同
+ self.pre = root
+ self.count = 1
+ if self.count > self.countMax: // 如果计数大于最大值频率
+ self.countMax = self.count // 更新最大频率
+ self.res = [root.val] //更新res
+ elif self.count == self.countMax: // 如果和最大值相同,放进res中
+ self.res.append(root.val)
+ findNumber(root.right) //右
+ return
+ findNumber(root)
+ return self.res
+```
Go:
@@ -405,4 +436,4 @@ Go:
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
-
\ No newline at end of file
+
From 03964a1f78a47710b2406d9a4ec00d3d73f0fddd Mon Sep 17 00:00:00 2001
From: Powerstot <77142630+Powerstot@users.noreply.github.com>
Date: Wed, 19 May 2021 22:26:03 +0800
Subject: [PATCH 02/12] =?UTF-8?q?=E4=BF=AE=E6=94=B91047.=E5=88=A0=E9=99=A4?=
=?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2=E4=B8=AD=E7=9A=84=E6=89=80=E6=9C=89?=
=?UTF-8?q?=E7=9B=B8=E9=82=BB=E9=87=8D=E5=A4=8D=E9=A1=B9=20Java=E7=89=88?=
=?UTF-8?q?=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
修改1047.删除字符串中的所有相邻重复项 Java版本
---
...除字符串中的所有相邻重复项.md | 26 +++++++++++++++++++
1 file changed, 26 insertions(+)
diff --git a/problems/1047.删除字符串中的所有相邻重复项.md b/problems/1047.删除字符串中的所有相邻重复项.md
index 607a4ccf..32a29c2a 100644
--- a/problems/1047.删除字符串中的所有相邻重复项.md
+++ b/problems/1047.删除字符串中的所有相邻重复项.md
@@ -122,6 +122,8 @@ public:
Java:
+
+使用 Deque 作为堆栈
```Java
class Solution {
public String removeDuplicates(String S) {
@@ -144,6 +146,30 @@ class Solution {
}
}
```
+拿字符串直接作为栈,省去了栈还要转为字符串的操作。
+```Java
+class Solution {
+ public String removeDuplicates(String s) {
+ // 将 res 当做栈
+ StringBuffer res = new StringBuffer();
+ // top为 res 的长度
+ int top = -1;
+ for (int i = 0; i < s.length(); i++) {
+ char c = s.charAt(i);
+ // 当 top > 0,即栈中有字符时,当前字符如果和栈中字符相等,弹出栈顶字符,同时 top--
+ if (top >= 0 && res.charAt(top) == c) {
+ res.deleteCharAt(top);
+ top--;
+ // 否则,将该字符 入栈,同时top++
+ } else {
+ res.append(c);
+ top++;
+ }
+ }
+ return res.toString();
+ }
+}
+```
Python:
```python3
From 8efc21f92d6485b52d7d24391da9d88090c5bd59 Mon Sep 17 00:00:00 2001
From: LiangDazhu <42199191+LiangDazhu@users.noreply.github.com>
Date: Wed, 19 May 2021 23:23:38 +0800
Subject: [PATCH 03/12] =?UTF-8?q?Update=200452.=E7=94=A8=E6=9C=80=E5=B0=91?=
=?UTF-8?q?=E6=95=B0=E9=87=8F=E7=9A=84=E7=AE=AD=E5=BC=95=E7=88=86=E6=B0=94?=
=?UTF-8?q?=E7=90=83.md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Added python version code
---
.../0452.用最少数量的箭引爆气球.md | 18 +++++++++++++++---
1 file changed, 15 insertions(+), 3 deletions(-)
diff --git a/problems/0452.用最少数量的箭引爆气球.md b/problems/0452.用最少数量的箭引爆气球.md
index f62fb153..45c94a01 100644
--- a/problems/0452.用最少数量的箭引爆气球.md
+++ b/problems/0452.用最少数量的箭引爆气球.md
@@ -70,7 +70,7 @@
其实都可以!只不过对应的遍历顺序不同,我就按照气球的起始位置排序了。
-既然按照其实位置排序,那么就从前向后遍历气球数组,靠左尽可能让气球重复。
+既然按照起始位置排序,那么就从前向后遍历气球数组,靠左尽可能让气球重复。
从前向后遍历遇到重叠的气球了怎么办?
@@ -167,7 +167,19 @@ class Solution {
```
Python:
-
+```python
+class Solution:
+ def findMinArrowShots(self, points: List[List[int]]) -> int:
+ if len(points) == 0: return 0
+ points.sort(key=lambda x: x[0])
+ result = 1
+ for i in range(1, len(points)):
+ if points[i][0] > points[i - 1][1]: # 气球i和气球i-1不挨着,注意这里不是>=
+ result += 1
+ else:
+ points[i][1] = min(points[i - 1][1], points[i][1]) # 更新重叠气球最小右边界
+ return result
+```
Go:
@@ -178,4 +190,4 @@ Go:
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
-
\ No newline at end of file
+
From ae6cd5a7ae35b4c1c838bbf967c2c27395591080 Mon Sep 17 00:00:00 2001
From: LiangDazhu <42199191+LiangDazhu@users.noreply.github.com>
Date: Wed, 19 May 2021 23:51:59 +0800
Subject: [PATCH 04/12] =?UTF-8?q?Update=200435.=E6=97=A0=E9=87=8D=E5=8F=A0?=
=?UTF-8?q?=E5=8C=BA=E9=97=B4.md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Added python version code
---
problems/0435.无重叠区间.md | 16 ++++++++++++++--
1 file changed, 14 insertions(+), 2 deletions(-)
diff --git a/problems/0435.无重叠区间.md b/problems/0435.无重叠区间.md
index 4341f3b8..540a82e3 100644
--- a/problems/0435.无重叠区间.md
+++ b/problems/0435.无重叠区间.md
@@ -212,7 +212,19 @@ class Solution {
```
Python:
-
+```python
+class Solution:
+ def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
+ if len(intervals) == 0: return 0
+ intervals.sort(key=lambda x: x[1])
+ count = 1 # 记录非交叉区间的个数
+ end = intervals[0][1] # 记录区间分割点
+ for i in range(1, len(intervals)):
+ if end <= intervals[i][0]:
+ count += 1
+ end = intervals[i][1]
+ return len(intervals) - count
+```
Go:
@@ -223,4 +235,4 @@ Go:
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
-
\ No newline at end of file
+
From 5155c815ed5bfdc1004d50da41cfcc2f66837567 Mon Sep 17 00:00:00 2001
From: jojoo15 <75017412+jojoo15@users.noreply.github.com>
Date: Thu, 20 May 2021 00:06:55 +0200
Subject: [PATCH 05/12] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200236.=20=E4=BA=8C?=
=?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E6=9C=80=E8=BF=91=E5=85=AC=E5=85=B1?=
=?UTF-8?q?=E7=A5=96=E5=85=88=20python=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
added python version of code
---
.../0236.二叉树的最近公共祖先.md | 20 +++++++++++++++++--
1 file changed, 18 insertions(+), 2 deletions(-)
diff --git a/problems/0236.二叉树的最近公共祖先.md b/problems/0236.二叉树的最近公共祖先.md
index 3233c6a1..a32c017e 100644
--- a/problems/0236.二叉树的最近公共祖先.md
+++ b/problems/0236.二叉树的最近公共祖先.md
@@ -263,8 +263,24 @@ class Solution {
```
Python:
-
-
+```python
+# Definition for a binary tree node.
+# class TreeNode:
+# def __init__(self, x):
+# self.val = x
+# self.left = None
+# self.right = None
+//递归
+class Solution:
+ def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
+ if not root or root == p or root == q: return root //找到了节点p或者q,或者遇到空节点
+ left = self.lowestCommonAncestor(root.left,p,q) //左
+ right = self.lowestCommonAncestor(root.right,p,q) //右
+ if left and right: return root //中: left和right不为空,root就是最近公共节点
+ elif left and not right: return left //目标节点是通过left返回的
+ elif not left and right: return right //目标节点是通过right返回的
+ else: return None //没找到
+```
Go:
```Go
func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
From 15824a1c2b95cf3811a5cd478fb84a844a7ac35d Mon Sep 17 00:00:00 2001
From: fusunx <1102654482@qq.com>
Date: Thu, 20 May 2021 08:02:34 +0800
Subject: [PATCH 06/12] =?UTF-8?q?0332.=E9=87=8D=E6=96=B0=E5=AE=89=E6=8E=92?=
=?UTF-8?q?=E8=A1=8C=E7=A8=8B=20Javascript?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0332.重新安排行程.md | 43 +++++++++++++++++++++++++++++
1 file changed, 43 insertions(+)
diff --git a/problems/0332.重新安排行程.md b/problems/0332.重新安排行程.md
index 756ecc86..0260a34b 100644
--- a/problems/0332.重新安排行程.md
+++ b/problems/0332.重新安排行程.md
@@ -399,6 +399,49 @@ char ** findItinerary(char *** tickets, int ticketsSize, int* ticketsColSize, in
}
```
+Javascript:
+```Javascript
+
+var findItinerary = function(tickets) {
+ let result = ['JFK']
+ let map = {}
+
+ for (const tickt of tickets) {
+ const [from, to] = tickt
+ if (!map[from]) {
+ map[from] = []
+ }
+ map[from].push(to)
+ }
+
+ for (const city in map) {
+ // 对到达城市列表排序
+ map[city].sort()
+ }
+ function backtracing() {
+ if (result.length === tickets.length + 1) {
+ return true
+ }
+ if (!map[result[result.length - 1]] || !map[result[result.length - 1]].length) {
+ return false
+ }
+ for(let i = 0 ; i < map[result[result.length - 1]].length; i++) {
+ let city = map[result[result.length - 1]][i]
+ // 删除已走过航线,防止死循环
+ map[result[result.length - 1]].splice(i, 1)
+ result.push(city)
+ if (backtracing()) {
+ return true
+ }
+ result.pop()
+ map[result[result.length - 1]].splice(i, 0, city)
+ }
+ }
+ backtracing()
+ return result
+};
+
+```
-----------------------
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
From 30a39b71f503f57b4feb1020203d0af7d22de77c Mon Sep 17 00:00:00 2001
From: xll <18574553598@163.com>
Date: Thu, 20 May 2021 13:10:26 +0800
Subject: [PATCH 07/12] =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91=E7=9A=84?=
=?UTF-8?q?=E9=80=92=E5=BD=92=E9=81=8D=E5=8E=86JavaScript=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/二叉树的递归遍历.md | 53 ++++++++++++++++++++++++++++
1 file changed, 53 insertions(+)
diff --git a/problems/二叉树的递归遍历.md b/problems/二叉树的递归遍历.md
index 2d571294..e5279ec0 100644
--- a/problems/二叉树的递归遍历.md
+++ b/problems/二叉树的递归遍历.md
@@ -306,6 +306,59 @@ var postorderTraversal = function(root, res = []) {
return res;
};
```
+Javascript版本:
+
+前序遍历:
+```Javascript
+var preorderTraversal = function(root) {
+ let res=[];
+ const dfs=function(root){
+ if(root===null)return ;
+ //先序遍历所以从父节点开始
+ res.push(root.val);
+ //递归左子树
+ dfs(root.left);
+ //递归右子树
+ dfs(root.right);
+ }
+ //只使用一个参数 使用闭包进行存储结果
+ dfs(root);
+ return res;
+};
+```
+中序遍历
+```javascript
+var inorderTraversal = function(root) {
+ let res=[];
+ const dfs=function(root){
+ if(root===null){
+ return ;
+ }
+ dfs(root.left);
+ res.push(root.val);
+ dfs(root.right);
+ }
+ dfs(root);
+ return res;
+};
+```
+
+后序遍历
+```javascript
+var postorderTraversal = function(root) {
+ let res=[];
+ const dfs=function(root){
+ if(root===null){
+ return ;
+ }
+ dfs(root.left);
+ dfs(root.right);
+ res.push(root.val);
+ }
+ dfs(root);
+ return res;
+};
+```
From ed557367383ab0df57e47074fcd751bcaba2308d Mon Sep 17 00:00:00 2001
From: reoaah
Date: Thu, 20 May 2021 14:37:47 +0800
Subject: [PATCH 08/12] =?UTF-8?q?=E6=B7=BB=E5=8A=A00509.=E6=96=90=E6=B3=A2?=
=?UTF-8?q?=E9=82=A3=E5=A5=91=E6=95=B0.md=20Go=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0509.斐波那契数.md | 13 +++++++++++++
1 file changed, 13 insertions(+)
diff --git a/problems/0509.斐波那契数.md b/problems/0509.斐波那契数.md
index 112c96f9..5afcd6b8 100644
--- a/problems/0509.斐波那契数.md
+++ b/problems/0509.斐波那契数.md
@@ -207,6 +207,19 @@ class Solution:
```
Go:
+```Go
+func fib(n int) int {
+ if n < 2 {
+ return n
+ }
+ a, b, c := 0, 1, 0
+ for i := 1; i < n; i++ {
+ c = a + b
+ a, b = b, c
+ }
+ return c
+}
+```
From e67b7acaecb03440cf644930a69f9f45bff04743 Mon Sep 17 00:00:00 2001
From: reoaah
Date: Thu, 20 May 2021 15:25:52 +0800
Subject: [PATCH 09/12] =?UTF-8?q?=E6=B7=BB=E5=8A=A00746.=E4=BD=BF=E7=94=A8?=
=?UTF-8?q?=E6=9C=80=E5=B0=8F=E8=8A=B1=E8=B4=B9=E7=88=AC=E6=A5=BC=E6=A2=AF?=
=?UTF-8?q?.md=20Go=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0746.使用最小花费爬楼梯.md | 16 ++++++++++++++++
1 file changed, 16 insertions(+)
diff --git a/problems/0746.使用最小花费爬楼梯.md b/problems/0746.使用最小花费爬楼梯.md
index b8158205..aff56208 100644
--- a/problems/0746.使用最小花费爬楼梯.md
+++ b/problems/0746.使用最小花费爬楼梯.md
@@ -228,7 +228,23 @@ Python:
Go:
+```Go
+func minCostClimbingStairs(cost []int) int {
+ dp := make([]int, len(cost))
+ dp[0], dp[1] = cost[0], cost[1]
+ for i := 2; i < len(cost); i++ {
+ dp[i] = min(dp[i-1], dp[i-2]) + cost[i]
+ }
+ return min(dp[len(cost)-1], dp[len(cost)-2])
+}
+func min(a, b int) int {
+ if a < b {
+ return a
+ }
+ return b
+}
+```
From e4fddadb78b17a27395b9341fec749782b80b274 Mon Sep 17 00:00:00 2001
From: jojoo15 <75017412+jojoo15@users.noreply.github.com>
Date: Thu, 20 May 2021 11:18:33 +0200
Subject: [PATCH 10/12] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200235.=E4=BA=8C?=
=?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E6=9C=80=E8=BF=91=E5=85=AC=E5=85=B1?=
=?UTF-8?q?=E7=A5=96=E5=85=88=20python=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
添加 0235.二叉树的最近公共祖先 python版本
---
...35.二叉搜索树的最近公共祖先.md | 19 +++++++++++++++++--
1 file changed, 17 insertions(+), 2 deletions(-)
diff --git a/problems/0235.二叉搜索树的最近公共祖先.md b/problems/0235.二叉搜索树的最近公共祖先.md
index 93642de5..64e9de3f 100644
--- a/problems/0235.二叉搜索树的最近公共祖先.md
+++ b/problems/0235.二叉搜索树的最近公共祖先.md
@@ -247,8 +247,23 @@ class Solution {
```
Python:
+```python
+# Definition for a binary tree node.
+# class TreeNode:
+# def __init__(self, x):
+# self.val = x
+# self.left = None
+# self.right = None
-
+class Solution:
+ def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
+ if not root: return root //中
+ if root.val >p.val and root.val > q.val:
+ return self.lowestCommonAncestor(root.left,p,q) //左
+ elif root.val < p.val and root.val < q.val:
+ return self.lowestCommonAncestor(root.right,p,q) //右
+ else: return root
+```
Go:
@@ -258,4 +273,4 @@ Go:
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
-
\ No newline at end of file
+
From 398e2b754e4da7ec4291448f7922fdb258a7d5b1 Mon Sep 17 00:00:00 2001
From: weijiew <49638002+weijiew@users.noreply.github.com>
Date: Thu, 20 May 2021 19:55:25 +0800
Subject: [PATCH 11/12] =?UTF-8?q?Update=200494.=E7=9B=AE=E6=A0=87=E5=92=8C?=
=?UTF-8?q?.md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0494.目标和.md | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/problems/0494.目标和.md b/problems/0494.目标和.md
index 1782c88c..65d9b4e2 100644
--- a/problems/0494.目标和.md
+++ b/problems/0494.目标和.md
@@ -225,7 +225,7 @@ public:
是的,如果仅仅是求个数的话,就可以用dp,但[回溯算法:39. 组合总和](https://mp.weixin.qq.com/s/FLg8G6EjVcxBjwCbzpACPw)要求的是把所有组合列出来,还是要使用回溯法爆搜的。
-本地还是有点难度,大家也可以记住,在求装满背包有几种方法的情况下,递推公式一般为:
+本题还是有点难度,大家也可以记住,在求装满背包有几种方法的情况下,递推公式一般为:
```
dp[j] += dp[j - nums[i]];
@@ -272,4 +272,4 @@ Go:
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
-
\ No newline at end of file
+
From df2e37ba90bee63ef90fc2b73f4f1ddd80f45828 Mon Sep 17 00:00:00 2001
From: jojoo15 <75017412+jojoo15@users.noreply.github.com>
Date: Thu, 20 May 2021 15:19:31 +0200
Subject: [PATCH 12/12] =?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=E7=A7=8D?=
=?UTF-8?q?=E7=9A=84=E8=8A=82=E7=82=B9=20python=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
添加 0450.删除二叉搜索树种的节点 python版本
---
.../0450.删除二叉搜索树中的节点.md | 40 ++++++++++++++++++-
1 file changed, 38 insertions(+), 2 deletions(-)
diff --git a/problems/0450.删除二叉搜索树中的节点.md b/problems/0450.删除二叉搜索树中的节点.md
index 604fb376..2cac8f87 100644
--- a/problems/0450.删除二叉搜索树中的节点.md
+++ b/problems/0450.删除二叉搜索树中的节点.md
@@ -281,7 +281,43 @@ class Solution {
```
Python:
-
+```python
+# Definition for a binary tree node.
+# class TreeNode:
+# def __init__(self, val=0, left=None, right=None):
+# self.val = val
+# self.left = left
+# self.right = right
+class Solution:
+ def deleteNode(self, root: TreeNode, key: int) -> TreeNode:
+ if not root: return root #第一种情况:没找到删除的节点,遍历到空节点直接返回了
+ if root.val == key:
+ if not root.left and not root.right: #第二种情况:左右孩子都为空(叶子节点),直接删除节点, 返回NULL为根节点
+ del root
+ return None
+ if not root.left and root.right: #第三种情况:其左孩子为空,右孩子不为空,删除节点,右孩子补位 ,返回右孩子为根节点
+ tmp = root
+ root = root.right
+ del tmp
+ return root
+ if root.left and not root.right: #第四种情况:其右孩子为空,左孩子不为空,删除节点,左孩子补位,返回左孩子为根节点
+ tmp = root
+ root = root.left
+ del tmp
+ return root
+ else: #第五种情况:左右孩子节点都不为空,则将删除节点的左子树放到删除节点的右子树的最左面节点的左孩子的位置
+ v = root.right
+ while v.left:
+ v = v.left
+ v.left = root.left
+ tmp = root
+ root = root.right
+ del tmp
+ return root
+ if root.val > key: root.left = self.deleteNode(root.left,key) #左递归
+ if root.val < key: root.right = self.deleteNode(root.right,key) #右递归
+ return root
+```
Go:
```Go
@@ -330,4 +366,4 @@ func deleteNode1(root *TreeNode)*TreeNode{
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
-
\ No newline at end of file
+