代码随想录训练营Day23|669. 修剪二叉搜索树108.将有序数组转换为二叉搜索树538.把二叉搜索树转换为累加树 总结篇

目录

学习目标

学习内容

669. 修剪二叉搜索树 

 108.将有序数组转换为二叉搜索树

538.把二叉搜索树转换为累加树 

 总结篇


学习目标

  • 669. 修剪二叉搜索树
  • 108.将有序数组转换为二叉搜索树
  • 538.把二叉搜索树转换为累加树
  • 总结篇

学习内容

669. 修剪二叉搜索树 

669. 修剪二叉搜索树 - 力扣(LeetCode)icon-default.png?t=N3I4https://leetcode.cn/problems/trim-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 trimBST(self, root: Optional[TreeNode], low: int, high: int) -> Optional[TreeNode]:
        if not root:return root
        if root.val<low:
            root.right = self.trimBST(root.right,low,high)
            return root.right
        elif root.val>high:
            root.left = self.trimBST(root.left,low,high)
            return root.left
        else:
            root.left = self.trimBST(root.left,low,high)
            root.right = self.trimBST(root.right,low,high)
            return root

 108.将有序数组转换为二叉搜索树

108. 将有序数组转换为二叉搜索树 - 力扣(LeetCode)icon-default.png?t=N3I4https://leetcode.cn/problems/convert-sorted-array-to-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 sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]:
        n = len(nums)
        if n==0:return 
        index = n//2
        root = TreeNode(val = nums[index])
        root.left = self.sortedArrayToBST(nums[0:index])
        root.right = self.sortedArrayToBST(nums[index+1:n])
        return root

538.把二叉搜索树转换为累加树 

538. 把二叉搜索树转换为累加树 - 力扣(LeetCode)icon-default.png?t=N3I4https://leetcode.cn/problems/convert-bst-to-greater-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 convertBST(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
        def tree(root):
            nonlocal amount
            if not root:return root
            root.right = tree(root.right)
            amount += root.val
            root.val = amount
            root.left = tree(root.left)
            return root
        amount = 0
        return tree(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 convertBST(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
        if not root:return root
        amount =0 
        stack = []
        cur = root
        while cur or stack:
            while cur:
                stack.append(cur)
                cur = cur.right
            cur = stack.pop()
            amount +=cur.val
            cur.val = amount
            cur = cur.left
        return root

 Morris解法

# 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 convertBST(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
        if not root:return root
        amount = 0
        pre = root
        cur = root
        while pre:
            # 连接根节点和右子树的最左侧叶子
            if pre.right:
                cur = pre.right
                while cur.left and cur.left!=pre:
                    cur = cur.left
                if not cur.left:
                    cur.left = pre
                    pre = pre.right
                elif cur.left==pre:
                    amount+=pre.val
                    pre.val = amount
                    cur.left = None
                    pre = pre.left
            else:
                amount+=pre.val
                pre.val = amount
                pre = pre.left
        return root

 总结篇

problems/二叉树总结篇.md · programmercarl/leetcode-master(代码随想录出品) - Gitee.comicon-default.png?t=N3I4https://gitee.com/programmercarl/leetcode-master/blob/master/problems/%E4%BA%8C%E5%8F%89%E6%A0%91%E6%80%BB%E7%BB%93%E7%AF%87.md

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值