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

This commit is contained in:
jianghongcheng
2023-05-23 21:38:27 -05:00
committed by GitHub
parent 511bf44793
commit 731d1ca8aa

View File

@ -324,88 +324,90 @@ class Solution {
``` ```
## Python ## Python
递归法(版本一)
```python ```python
class Solution: class Solution:
def deleteNode(self, root: Optional[TreeNode], key: int) -> Optional[TreeNode]: def deleteNode(self, root, key):
if not root : return None # 节点为空,返回 if root is None:
if root.val < key : return root
root.right = self.deleteNode(root.right, key) if root.val == key:
elif root.val > key : if root.left is None and root.right is None:
return None
elif root.left is None:
return root.right
elif root.right is None:
return root.left
else:
cur = root.right
while cur.left is not None:
cur = cur.left
cur.left = root.left
return root.right
if root.val > key:
root.left = self.deleteNode(root.left, key) root.left = self.deleteNode(root.left, key)
else: if root.val < key:
# 当前节点的左子树为空,返回当前的右子树 root.right = self.deleteNode(root.right, key)
if not root.left : return root.right
# 当前节点的右子树为空,返回当前的左子树
if not root.right: return root.left
# 左右子树都不为空,找到右孩子的最左节点 记为p
node = root.right
while node.left :
node = node.left
# 将当前节点的左子树挂在p的左孩子上
node.left = root.left
# 当前节点的右子树替换掉当前节点,完成当前节点的删除
root = root.right
return root return root
``` ```
**普通二叉树的删除方式** 递归法(版本二)
```python ```python
class Solution: class Solution:
def deleteNode(self, root: Optional[TreeNode], key: int) -> Optional[TreeNode]: def deleteNode(self, root, key):
if not root: return root if root is None: # 如果根节点为空,直接返回
if root.val == key: return root
if not root.right: # 这里第二次操作目标值:最终删除的作用 if root.val == key: # 找到要删除的节点
if root.right is None: # 如果右子树为空,直接返回左子树作为新的根节点
return root.left return root.left
tmp = root.right cur = root.right
while tmp.left: while cur.left: # 找到右子树中的最左节点
tmp = tmp.left cur = cur.left
root.val, tmp.val = tmp.val, root.val # 这里第一次操作目标值:交换目标值其右子树最左节点 root.val, cur.val = cur.val, root.val # 将要删除的节点值与最左节点值交换
root.left = self.deleteNode(root.left, key) # 在左子树中递归删除目标节点
root.left = self.deleteNode(root.left, key) root.right = self.deleteNode(root.right, key) # 在右子树中递归删除目标节点
root.right = self.deleteNode(root.right, key)
return root return root
``` ```
**迭代法** **迭代法**
```python ```python
class Solution: class Solution:
def deleteNode(self, root: Optional[TreeNode], key: int) -> Optional[TreeNode]: def deleteOneNode(self, target: TreeNode) -> TreeNode:
# 找到节点后分两步1. 把节点的左子树和右子树连起来2. 把右子树跟父节点连起来 """
# root is None 将目标节点(删除节点)的左子树放到目标节点的右子树的最左面节点的左孩子位置上
if not root: return root 并返回目标节点右孩子为新的根节点
p = root 是动画里模拟的过程
last = None """
while p: if target is None:
if p.val==key: return target
# 1. connect left to right if target.right is None:
# right is not None -> left is None | left is not None return target.left
if p.right: cur = target.right
if p.left: while cur.left:
node = p.right cur = cur.left
while node.left: cur.left = target.left
node = node.left return target.right
node.left = p.left
right = p.right def deleteNode(self, root: TreeNode, key: int) -> TreeNode:
else: if root is None:
# right is None -> right=left return root
right = p.left cur = root
# 2. connect right to last pre = None # 记录cur的父节点用来删除cur
if last==None: while cur:
root = right if cur.val == key:
elif last.val>key:
last.left = right
else:
last.right = right
# 3. return
break break
pre = cur
if cur.val > key:
cur = cur.left
else: else:
# Update last and continue cur = cur.right
last = p if pre is None: # 如果搜索树只有头结点
if p.val>key: return self.deleteOneNode(cur)
p = p.left # pre 要知道是删左孩子还是右孩子
else: if pre.left and pre.left.val == key:
p = p.right pre.left = self.deleteOneNode(cur)
if pre.right and pre.right.val == key:
pre.right = self.deleteOneNode(cur)
return root return root
``` ```