算法训练营Day22(二叉树8)

235. 二叉搜索树的最近公共祖先力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台

提醒

相对于 二叉树的最近公共祖先 本题就简单一些了,因为 可以利用二叉搜索树的特性。

递归法

class Solution:
    def traversal(self, cur, p, q):
        if cur is None:
            return cur
        # 左                                             
        if cur.val > p.val and cur.val > q.val:           
            left = self.traversal(cur.left, p, q)
            #中
            if left is not None:
                return left
        # 右
        elif cur.val < p.val and cur.val < q.val:           
            right = self.traversal(cur.right, p, q)
            #中
            if right is not None:
                return right
        else:
            return cur
    def lowestCommonAncestor(self, root, p, q):
        return self.traversal(root, p, q)

迭代法

class Solution:
    def lowestCommonAncestor(self, root, p, q):
        while root:
            if root.val > p.val and root.val > q.val:
                root = root.left
            elif root.val < p.val and root.val < q.val:
                root = root.right
            else:
                return root
        return None

递归法 

class Solution:
    def insertIntoBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]:
        if root is None    #为什么不加上or root.val == val:  #因为这是搜索二叉树
            return TreeNode(val)
        elif root.val > val:
            root.left=self.insertIntoBST(root.left, val)
        elif root.val < val:
            root.right =self.insertIntoBST(root.right, val)
        return root

迭代法

class Solution:
    def insertIntoBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]:
        if root is None:  # 如果根节点为空,创建新节点作为根节点并返回
            return TreeNode(val)
        cur = root
        parent = root  # 记录上一个节点,用于连接新节点
        while cur is not None:
            parent = cur
            if cur.val > val:
                cur = cur.left
            else:
                cur = cur.right

        node = TreeNode(val)
        if val < parent.val:
            parent.left = node  # 将新节点连接到父节点的左子树
        else:
            parent.right = node  # 将新节点连接到父节点的右子树
        return root

 

450.删除二叉搜索树中的节点 力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台

提醒

 插入操作,本题就有难度了,涉及到改树的结构,把二叉搜索树中删除节点遇到的情况都搞清楚,有以下五种情况:

  • 第一种情况:没找到删除的节点,遍历到空节点直接返回了找到删除的节点
  • 第二种情况:左空右空(叶子节点),直接删除节点, 返回NULL为根节点
  • 第三种情况:删除节点的左为空,右不为空,删除节点,右孩子补位,返回让右孩子为根节点
  • 第四种情况:删除节点的右孩子为空,左孩子不为空,删除节点,左孩子补位,返回左孩子为根节点
  • 第五种情况:左右孩子节点都不为空,则将删除节点的左子树头结点(左孩子)放到删除节点的右子树的最左面节点的左孩子上,返回删除节点右孩子为新的根节点。

搜索二叉树

class Solution:
    def deleteNode(self, root: Optional[TreeNode], key: int) -> Optional[TreeNode]:
        if root is None:
            return root
        if 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
        #单层逻辑   这里面的root与上面终止条件中的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

普通二叉树(进阶

二刷再来

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Best,

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值