Skip to content

Commit 988e9fc

Browse files
committed
Merge branch 'youngyangyang04:master' into master
2 parents c4c1ff2 + 88ec3e6 commit 988e9fc

File tree

3 files changed

+58
-7
lines changed

3 files changed

+58
-7
lines changed

problems/0235.二叉搜索树的最近公共祖先.md

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,23 @@ class Solution {
247247
```
248248

249249
Python:
250-
251-
250+
```python
251+
# Definition for a binary tree node.
252+
# class TreeNode:
253+
# def __init__(self, x):
254+
# self.val = x
255+
# self.left = None
256+
# self.right = None
257+
258+
class Solution:
259+
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
260+
if not root: return root //
261+
if root.val >p.val and root.val > q.val:
262+
return self.lowestCommonAncestor(root.left,p,q) //
263+
elif root.val < p.val and root.val < q.val:
264+
return self.lowestCommonAncestor(root.right,p,q) //
265+
else: return root
266+
```
252267
Go:
253268

254269

@@ -258,4 +273,4 @@ Go:
258273
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
259274
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
260275
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
261-
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
276+
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>

problems/0450.删除二叉搜索树中的节点.md

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,43 @@ class Solution {
281281
```
282282

283283
Python:
284-
284+
```python
285+
# Definition for a binary tree node.
286+
# class TreeNode:
287+
# def __init__(self, val=0, left=None, right=None):
288+
# self.val = val
289+
# self.left = left
290+
# self.right = right
291+
class Solution:
292+
def deleteNode(self, root: TreeNode, key: int) -> TreeNode:
293+
if not root: return root #第一种情况:没找到删除的节点,遍历到空节点直接返回了
294+
if root.val == key:
295+
if not root.left and not root.right: #第二种情况:左右孩子都为空(叶子节点),直接删除节点, 返回NULL为根节点
296+
del root
297+
return None
298+
if not root.left and root.right: #第三种情况:其左孩子为空,右孩子不为空,删除节点,右孩子补位 ,返回右孩子为根节点
299+
tmp = root
300+
root = root.right
301+
del tmp
302+
return root
303+
if root.left and not root.right: #第四种情况:其右孩子为空,左孩子不为空,删除节点,左孩子补位,返回左孩子为根节点
304+
tmp = root
305+
root = root.left
306+
del tmp
307+
return root
308+
else: #第五种情况:左右孩子节点都不为空,则将删除节点的左子树放到删除节点的右子树的最左面节点的左孩子的位置
309+
v = root.right
310+
while v.left:
311+
v = v.left
312+
v.left = root.left
313+
tmp = root
314+
root = root.right
315+
del tmp
316+
return root
317+
if root.val > key: root.left = self.deleteNode(root.left,key) #左递归
318+
if root.val < key: root.right = self.deleteNode(root.right,key) #右递归
319+
return root
320+
```
285321

286322
Go:
287323
```Go
@@ -330,4 +366,4 @@ func deleteNode1(root *TreeNode)*TreeNode{
330366
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
331367
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
332368
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
333-
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
369+
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>

problems/0494.目标和.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ public:
225225
226226
是的,如果仅仅是求个数的话,就可以用dp,但[回溯算法:39. 组合总和](https://mp.weixin.qq.com/s/FLg8G6EjVcxBjwCbzpACPw)要求的是把所有组合列出来,还是要使用回溯法爆搜的。
227227
228-
本地还是有点难度,大家也可以记住,在求装满背包有几种方法的情况下,递推公式一般为:
228+
本题还是有点难度,大家也可以记住,在求装满背包有几种方法的情况下,递推公式一般为:
229229
230230
```
231231
dp[j] += dp[j - nums[i]];
@@ -272,4 +272,4 @@ Go:
272272
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
273273
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
274274
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
275-
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
275+
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>

0 commit comments

Comments
 (0)