【代码随想录算法训练营】第22天 | 第六章 二叉树(八)

主要内容

  1. 二叉搜索树最近公共祖先
  2. 二叉搜索树插入
  3. 二叉搜索树删除

题目

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

思路分析

因为是有序的,从上到下遍历,只要cur.val 在[p.val, q.val] 之间,cur就是要返回的值
在这里插入图片描述

代码

class Solution:
    def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
        # 按普通二叉树的思路来呢
        if not root:
            return None
        if root == p or root == q:
            return root
        left = self.lowestCommonAncestor(root.left, p, q)
        right = self.lowestCommonAncestor(root.right, p, q)

        if left and right:
            return root
        elif not left and right:
            return right
        elif left and not right:
            return left
        else:
            return None
class Solution:
    def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
        # 递归,二叉搜索树
        if not root:
            return None
        if root.val > p.val and root.val > q.val:
            left = self.lowestCommonAncestor(root.left, p, q)
            if left:
                return left
        if root.val < p.val and root.val < q.val:
            right = self.lowestCommonAncestor(root.right, p, q)
            if right:
                return right
        return root
class Solution:
    def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
        # 迭代,二叉搜索树
        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

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

添加链接描述

思路分析

若节点不为空,返回的是当前节点,由上一层接住

代码

class Solution:
    def insertIntoBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]:
        if root is None:
            return TreeNode(val)
        if root.val > val:
            root.left = self.insertIntoBST(root.left, val)
        if root.val < val:
            root.right = self.insertIntoBST(root.right, val)
        return root        
    def insertIntoBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]:
        node = TreeNode(val)
        if root is None:
            return node
        if root.left is None and root.val > val:
            root.left = node
        if root.right is None and root.val < val:
            root.right = node

        if root.val > val:
            self.insertIntoBST(root.left, val)
        if root.val < val:
            self.insertIntoBST(root.right, val)

        return root 

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

添加链接描述

思路分析

第一遍自己的思路:迭代
递归:
一、遇到空节点,返回
二、cur.val == key:

  1. left 为空,right补上
  2. right 为空,left补上
  3. left,放在right左孩子左叶子节点后面

代码

class Solution:
    def deleteNode(self, root: Optional[TreeNode], key: int) -> Optional[TreeNode]:
        # 迭代
        if not root:
            return None
        if root.val == key:
            return self.newTree(root)
        cur = root    
        while cur:
            if cur.val > key:
                # 向左找
                if cur.left and cur.left.val == key:
                    cur.left = self.newTree(cur.left)
                    return root
                cur = cur.left
            elif cur.val < key:
                # 向右找
                if cur.right and cur.right.val == key:
                    cur.right = self.newTree(cur.right)
                    return root
                cur = cur.right
        return root                    

    def newTree(self, cur):
        # 将以cur为根的左右子树,重组为二叉搜索树
        if not cur.left and not cur.right:
            return None
        nums = []
        def _traversal(node):
            if not node:
                return
            _traversal(node.left)
            nums.append(node.val)
            _traversal(node.right)
        _traversal(cur)
        nums.remove(cur.val)
        r = TreeNode(nums[0])
        p = r
        for i in range(1, len(nums)):
            p.right = TreeNode(nums[i])
            p = p.right
        return r        

标准做法

class Solution:
    def deleteNode(self, root: Optional[TreeNode], key: int) -> Optional[TreeNode]:
        # 递归
        if not root:
            return None
        if root.val == key:
            # 左孩子为空
            if not root.left:
                return root.right
            # 右孩子为空
            elif not root.right:
                return root.left
            # 均不为空
            else:
                tmp = root.right
                # 找到左叶子
                while tmp.left:
                    tmp = tmp.left
                tmp.left = root.left
                return root.right
        if root.val < key:
            root.right = self.deleteNode(root.right, key)
        if root.val > key:
            root.left = self.deleteNode(root.left, key)
        return root    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值