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 1/3] =?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 2/3] =?UTF-8?q?Update=200494.=E7=9B=AE=E6=A0=87=E5=92=8C.m?=
=?UTF-8?q?d?=
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 3/3] =?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
+