代码随想录训练营第III期--022+023--python

不想搞的两天,摆烂的两天

# 代码随想录训练营第III期--022--python

# 235 二叉搜索树的最近公共祖先
class Solution:
    def lowest(self, root,p,q):
        ancestor = root 
        while True:
            if ancestor.val > p.val and ancestor.val > q.val:
                ancestor = ancestor.left 
            elif ancestor.val < p.val and ancestor.val < q.val:
                ancestor = ancestor.right 
            else:
                break 
        return ancestor 

# 701
    def insert(self, root, val):
        if not root:
            return TreeNode(val)
        cur = root
        while cur:
            if val < cur.val:
                if not cur.left:
                    cur.left = TreeNode(val)
                    break 
                else:
                    cur = cur.left 
            else:
                if not cur.right:
                    cur.right = TreeNode(val)
                    break 
                else:
                    cur = cur.right 
        return root 

# 450
    def deleteNode(self, root, key):
        if not root: return root 
        if root.val > key:
            root.left = self.deleteNode(root.left, key)
            return root 
        elif root.val < key:
            root.right = self.deleteNode(root.right, key)
            return root 
        else:
            if not root.left:
                return root.right 
            elif not root.right:
                return root.left 
            else:
                curr = root.right 
                while curr.left:
                    curr = curr.left 
                curr.left = root.left 
                return root.right

# =================================================
# 代码随想录训练营第III期--023--python
# 真实不想做二叉树了,做不动了,完全没想法

# 669
class Solution:
    def trimBST(self, root, low, high):
        if not root:
            return None 
        if root.val < low:
            right = self.trimBST(root.right, low, high)
            return right 
        if root.val > high:
            left = self.trimBST(root.left, low, high)
            return left 

        root.left = self.trimBST(root.left, low, high)
        root.right = self.trimBST(root.right, low, high)

        return root 

# 108
# this question is quite interesting
    def sortedArrayToBST(self, nums):
        def build(left, right):
            if left > right:
                return 
            mid = left + (right - left) // 2
            root = TreeNode(nums[mid])
            root.left = build(left, mid - 1)
            root.right = build(mid + 1, right)
            return root 
        return build(0, len(nums) - 1)

# 538
    pre = 0 
    def createBinaryTre(self, root):
        if not root: return 
        self.createBinaryTre(root.right)
        root.val += self.pre 
        self.pre = root.val 
        self.createBinaryTre(root.left)

    def convertBST(self, root):
        self.pre = 0 
        self. createBinaryTre(root)
        return root 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

deyiwang89

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

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

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

打赏作者

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

抵扣说明:

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

余额充值