Merge branch 'youngyangyang04:master' into master

This commit is contained in:
xllpiupiu
2021-05-21 11:23:42 +08:00
3 changed files with 57 additions and 6 deletions

View File

@ -247,8 +247,23 @@ class Solution {
``` ```
Python 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 Go

View File

@ -281,7 +281,43 @@ class Solution {
``` ```
Python 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
```Go ```Go

View File

@ -225,7 +225,7 @@ public:
是的如果仅仅是求个数的话就可以用dp但[回溯算法39. 组合总和](https://mp.weixin.qq.com/s/FLg8G6EjVcxBjwCbzpACPw)要求的是把所有组合列出来,还是要使用回溯法爆搜的。 是的如果仅仅是求个数的话就可以用dp但[回溯算法39. 组合总和](https://mp.weixin.qq.com/s/FLg8G6EjVcxBjwCbzpACPw)要求的是把所有组合列出来,还是要使用回溯法爆搜的。
还是有点难度,大家也可以记住,在求装满背包有几种方法的情况下,递推公式一般为: 还是有点难度,大家也可以记住,在求装满背包有几种方法的情况下,递推公式一般为:
``` ```
dp[j] += dp[j - nums[i]]; dp[j] += dp[j - nums[i]];