代码随想录训练营Day22|235. 二叉搜索树的最近公共祖先701.二叉搜索树中的插入操作450.删除二叉搜索树中的节点

目录

学习目标

学习内容

235. 二叉搜索树的最近公共祖先

 701.二叉搜索树中的插入操作

 450.删除二叉搜索树中的节点


学习目标

  • 235. 二叉搜索树的最近公共祖先
  • 701.二叉搜索树中的插入操作
  • 450.删除二叉搜索树中的节点

学习内容

235. 二叉搜索树的最近公共祖先

235. 二叉搜索树的最近公共祖先 - 力扣(LeetCode)icon-default.png?t=N3I4https://leetcode.cn/problems/lowest-common-ancestor-of-a-binary-search-tree/

# 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
        elif p.val>root.val and q.val>root.val:
            return self.lowestCommonAncestor(root.right,p,q)
        elif p.val

 701.二叉搜索树中的插入操作

701. 二叉搜索树中的插入操作 - 力扣(LeetCode)icon-default.png?t=N3I4https://leetcode.cn/problems/insert-into-a-binary-search-tree/

# 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 insertIntoBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]:
        new = TreeNode(val = val)
        if not root:return new
        cur = root
        while cur:
            if cur.val>val:
                if cur.left:
                    cur = cur.left
                else:
                    cur.left = new
                    break
            else:
                if cur.right:
                    cur = cur.right
                else:
                    cur.right = new
                    break
        return root

 450.删除二叉搜索树中的节点

450. 删除二叉搜索树中的节点 - 力扣(LeetCode)icon-default.png?t=N3I4https://leetcode.cn/problems/delete-node-in-a-bst/

  1. 删除节点无左右子树
  2. 删除节点无左子树
  3. 删除节点无右子树
  4. 删除节点有左右子树 

针对情况四 将节点的左子树变成右子树的最左叶子的左子树 

# 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: Optional[TreeNode], key: int) -> Optional[TreeNode]:
        if not root:return root
        if root.val==key:
            if not root.left:
                return root.right
            elif not root.right:
                return root.left
            else:
                cur = root.right
                while cur.left:
                    cur = cur.left
                cur.left = root.left
                return root.right 
        elif root.val>key:
            root.left = self.deleteNode(root.left,key)
        else:
            root.right = self.deleteNode(root.right,key)
        return root

 针对情况四 将删除节点与右子树的最左叶子的值交换,转移到情况一删除

# 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: Optional[TreeNode], key: int) -> Optional[TreeNode]:
        if not root:return root
        if root.val==key:
            if not root.left:
                return root.right # 这里第二次操作目标值:最终删除的作用
            elif not root.right:
                return root.left
            else:
                cur = root.right
                while cur.left:
                    pre = cur
                    cur = cur.left
                root.val,cur.val = cur.val,root.val # 这里第一次操作目标值:交换目标值其右子树最左面节点。
                root.right = self.deleteNode(root.right,key)
                return root
        elif root.val>key:
            root.left = self.deleteNode(root.left,key)
        else:
            root.right = self.deleteNode(root.right,key)
        return root

 

你可能感兴趣的:(算法,数据结构,leetcode,python)