Esempio n. 1
0
 def copy(self, source: TreeNode) -> TreeNode:
     if not source:
         return source
     res = TreeNode(source.val)
     res.left = self.copy(source.left)
     res.right = self.copy(source.right)
     return res
Esempio n. 2
0
 def build(self, values, index, n):
     if index <= n and index in values:
         node = TreeNode(values[index])
         node.left = self.build(values, 2 * index + 1, n)
         node.right = self.build(values, 2 * index + 2, n)
         return node
     else:
         return None
Esempio n. 3
0
 def insert(self, root: TreeNode, num: int) -> TreeNode:
     if not root:
         return TreeNode(num)
     if num < root.val:
         root.left = self.insert(root.left, num)
     else:
         root.right = self.insert(root.right, num)
     return root
 def __buildTree(self, inorder, postorder, idx_itr):
     if not inorder:
         return None
     pidx = next(idx_itr)
     root = TreeNode(postorder[pidx])
     idx = inorder.index(postorder[pidx])
     root.right = self.__buildTree(inorder[idx + 1:], postorder, idx_itr)
     root.left = self.__buildTree(inorder[:idx], postorder, idx_itr)
     return root
def build_tree(preorder: List[int], inorder: List[int]) -> Optional[TreeNode]:
    if not inorder:
        return None
    mid = inorder.index(preorder[0])
    root = TreeNode(preorder[0])
    if len(inorder) > 1:
        root.left = build_tree(preorder[1 : mid + 1], inorder[:mid])
        root.right = build_tree(preorder[mid + 1 :], inorder[mid + 1 :])
    return root
Esempio n. 6
0
def sorted_array_to_bst(nums: list[int]) -> Optional[TreeNode]:
    if not nums:
        return None

    middle = len(nums) // 2
    root = TreeNode(nums[middle])
    root.left = sorted_array_to_bst(nums[:middle])
    root.right = sorted_array_to_bst(nums[middle + 1 :])
    return root
Esempio n. 7
0
 def sorted_array_to_bst(self, nums, start, stop):
     if start <= stop:
         # 这里 + 1 保证如果对称的时候,取偏右,因为题目中数组是中序遍历的
         mid = start + (stop - start + 1) // 2
         node = TreeNode(nums[mid])
         node.left = self.sorted_array_to_bst(nums, start, mid - 1)
         node.right = self.sorted_array_to_bst(nums, mid + 1, stop)
         return node
     else:
         return None
Esempio n. 8
0
 def delete(self, root: TreeNode, num: int) -> TreeNode:
     if not root:
         return None
     if num == root.val:
         return None
     elif num < root.val:
         root.left = self.delete(root.left, num)
     else:
         root.right = self.delete(root.right, num)
     return root
 def build_bst(start: int, end: int) -> Optional[TreeNode]:
     nonlocal head
     if start > end:
         return None
     middle = (start + end) // 2
     left = build_bst(start, middle - 1)
     root = TreeNode(head.val)
     root.left = left
     head = head.next
     root.right = build_bst(middle + 1, end)
     return root
Esempio n. 10
0
File: 701.py Progetto: Cjz-Y/shuati
    def insertIntoBST(self, root: TreeNode, val: int) -> TreeNode:
        if not root:
            return TreeNode(val)

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

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

        return root
    def __buildTree(self, preorder_itr, inorder):
        if not inorder:
            return None
        try:
            curpre = next(preorder_itr)
        except StopIteration:
            return None

        idx = inorder.index(curpre)
        root = TreeNode(curpre)
        root.left = self.__buildTree(preorder_itr, inorder[:idx])
        root.right = self.__buildTree(preorder_itr, inorder[idx + 1:])
        return root
    def sortedListToBST(self, head):
        if not head:
            return None
        if not head.next:
            node = TreeNode(head.val)
            return node

        fast = head
        slow = head
        prev = None
        while fast.next and fast.next.next:
            fast = fast.next.next
            prev = slow
            slow = slow.next

        node = TreeNode(slow.val)
        right = slow.next
        if prev:
            prev.next = None
            node.left = self.sortedListToBST(head)
        else:
            node.left = self.sortedListToBST(None)
        node.right = self.sortedListToBST(right)
        return node
Esempio n. 13
0
    def helper(in_order: List[int],
               post_order: List[int]) -> Optional[TreeNode]:
        if in_order and post_order:
            root_val = post_order[-1]
            for root_offset, val in enumerate(in_order):
                if val == root_val:
                    break
            else:
                root_offset = 0

            root = TreeNode(root_val)
            root.left = helper(in_order[:root_offset],
                               post_order[:root_offset])
            root.right = helper(in_order[root_offset + 1:],
                                post_order[root_offset:-1])
            return root
    def buildTree(self, preorder, inorder):
        m = len(preorder)
        n = len(inorder)
        if m != n:
            return None
        if m == 0:
            return None

        val = preorder[0]
        for i, v in enumerate(inorder):
            if v == val:
                break
        head = TreeNode(val)
        head.left = self.buildTree(preorder[1:i+1], inorder[0:i])
        head.right = self.buildTree(preorder[i+1:], inorder[i+1:])
        return head
    def buildTree(self, inorder, postorder):
        m = len(inorder)
        n = len(postorder)
        if m != n:
            return None
        if m == 0:
            return None

        val = postorder[-1]
        for i, v in enumerate(inorder):
            if v == val:
                break
        head = TreeNode(val)
        head.left = self.buildTree(inorder[0:i], postorder[0:i])
        head.right = self.buildTree(inorder[i+1:], postorder[i:-1])
        return head
Esempio n. 16
0
    def sortedArrayToBST(self, nums: List[int]) -> TreeNode:
        """
        分析了一下,除了 root 可能有 right 其余的都是 left

        0
        算法不正确,原因在于题目要求的树是 DFS
        [-10,-3,0,5,9]
        5 是 9 的下一层,如果右侧按顺序,就成了 5 在 9 的上一层

        1
        分析发现,既然是 DF,相当于两树合并起来
        还是不对,理解错题目了,是要求平衡二叉树

        2
        感觉可以用索引来完成

        分析时间复杂度
        取切片时称动元素为基本操作
        1/2 n
            1/4 n
                1/8 n
                1/8 n
            1/4 n
                1/8 n
                1/8 n
        1/2 n
            ...

        可以看到每一轮共有 k 个 1/k n
        所以每一轮为 n
        而轮数为 log(n) 所以 T(n)=O(n logn)

        分析空间复杂度
        输出的树不考虑,那临时占用的就只有 mid 变量,与结点数一致
        S(n)=O(n)
        """
        if not nums:
            return None
        mid = len(nums) // 2
        root = TreeNode(nums[mid])
        # 原数组中序遍历而来,所以左右各能组成树
        if 0 < mid:
            root.left = self.sortedArrayToBST(nums[:mid])
        if mid + 1 < len(nums):
            root.right = self.sortedArrayToBST(nums[mid + 1:])
        return root
Esempio n. 17
0
 def invert(node: TreeNode) -> None:
     node.left, node.right = node.right, node.left
     if node.left:
         invert(node.left)
     if node.right:
         invert(node.right)