day21-binary tree-part08-7.23

tasks for today:

  1. 699.修建二叉搜索树

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

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


  1. 699.修建二叉搜索树

IN this practice, the feature of being binary search tree is very important, this can help trim the tree speedily.

when in "if root.val < low:" this condition, the left child tree can be totally trimed, because this is a binary search tree. All the left child value is less than the value of current root.

Note: Please compare the difference between this practice and the practice 450.

python 复制代码
# 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 root is None:
            return None
        
        if root.val < low:
            # when in this condition, the left child tree can be totally trimed, because this is a binary search tree
            return self.trimBST(root.right, low, high)
        elif root.val > high:
            # when in this condition, the right child tree can be totally trimed, because this is a binary search tree
            return self.trimBST(root.left, low, high)

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

        return root

one question: if I follow the logic of practice 450, it sometimes work well, but sometimes return fause solution, I mean: it is true that if a node's value is outside the range [low, high], the entire subtree rooted at that node needs to be trimmed, but I wonder, if I follow the logic of deleting a node to trim the tree, although it is more clumsy, but that should also work, the difference is I transaction the trimming a subtree into trimming nodes one-buy-one, why it sometimes not work, and sometimes work.

This problem might be created by the traverse order issue, because based on the example caser, some removed node is not correctly remove becasue some pitential trace back in the recursive algorithm. [2,1,3], low=3, high=4, [3] is expected but [3,1] is returned.

But this problem is not in delete node, maybe because the judging condition of root.val > key or root.val < key.

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

this practice's target is build a tree, so the key idea is to identify the val for construct current root node and the corresponding list feed into it for its following branches construction.

python 复制代码
# 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]:
        
        if not nums:
            return

        new_node = TreeNode(nums[int((len(nums)-1)/2)])

        new_node.left = self.sortedArrayToBST(nums[:int((len(nums)-1)/2)])
        new_node.right = self.sortedArrayToBST(nums[int((len(nums)-1)/2)+1:])
        
        return new_node
  1. 538.把二叉搜索树转化为累加树

for a binary seach tree, the inorder search is a ascending list, to calculate the sum of values larger than cur node, the inorder shoudl be reversed, which makes a descending list, by adding the nums before the value of current node, the cur tree node's value can be given to cur root node for upate.

Noted: when there are variables in recursive, a self.XXX definition should be necessary.

python 复制代码
# 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]:
        # when there is variable in the recursive, this from should be used
        self.pre = 0
        self.rev_inorder(root)

        return root

    def rev_inorder(self, node):
        if node is None:
            return
        
        self.rev_inorder(node.right)
        node.val += self.pre
        self.pre = node.val
        self.rev_inorder(node.left)
相关推荐
passer__jw7674 分钟前
【LeetCode】【算法】283. 移动零
数据结构·算法·leetcode
Ocean☾11 分钟前
前端基础-html-注册界面
前端·算法·html
顶呱呱程序19 分钟前
2-143 基于matlab-GUI的脉冲响应不变法实现音频滤波功能
算法·matlab·音视频·matlab-gui·音频滤波·脉冲响应不变法
爱吃生蚝的于勒40 分钟前
深入学习指针(5)!!!!!!!!!!!!!!!
c语言·开发语言·数据结构·学习·计算机网络·算法
羊小猪~~44 分钟前
数据结构C语言描述2(图文结合)--有头单链表,无头单链表(两种方法),链表反转、有序链表构建、排序等操作,考研可看
c语言·数据结构·c++·考研·算法·链表·visual studio
王哈哈^_^1 小时前
【数据集】【YOLO】【VOC】目标检测数据集,查找数据集,yolo目标检测算法详细实战训练步骤!
人工智能·深度学习·算法·yolo·目标检测·计算机视觉·pyqt
星沁城1 小时前
240. 搜索二维矩阵 II
java·线性代数·算法·leetcode·矩阵
脉牛杂德1 小时前
多项式加法——C语言
数据结构·c++·算法
legend_jz1 小时前
STL--哈希
c++·算法·哈希算法
kingmax542120082 小时前
初三数学,最优解问题
算法