Exemple #1
0
 def invertTree(self, root: TreeNode) -> TreeNode:
     if root is None:
         return None
     temp = root.right
     root.right = self.invertTree(root.left)
     root.left = self.invertTree(temp)
     return root
 def mirrorTree(self, root: TreeNode) -> TreeNode:
     # 为空返回
     if not root: return
     # 保存左节点,就像a,b互换值需要一个临时变量
     temp = root.left
     root.left = self.mirrorTree(root.right)
     root.right = self.mirrorTree(temp)
     return root
Exemple #3
0
    def insertIntoBST(self, root: TreeNode, val: int) -> TreeNode:
        if not root:
            return TreeNode(val)
        if root.val >= val:
            root.left = self.insertIntoBST(root.left, val)
        else:
            root.right = self.insertIntoBST(root.right, val)

        return root
 def helper(preorder: List[int], inorder: List[int], preleft: int, preright: int, inleft: int, inright: int):
     if inleft > inright:
         return None
     val = preorder[preleft]
     mid = inorder.index(val)
     root = TreeNode(val)
     root.left = helper(preorder, inorder, preleft + 1, mid - inleft + preleft, inleft, mid - 1)
     root.right = helper(preorder, inorder, mid - inleft + preleft + 1, preright, mid + 1, inright)
     return root
 def buildTree_(self, preorder: List[int], inorder: List[int]) -> TreeNode:
     if not preorder:
         return None
     val = preorder[0]
     k = inorder.index(val)
     root = TreeNode(val)
     root.left = self.buildTree(preorder[1:k + 1], inorder[:k])
     root.right = self.buildTree(preorder[k + 1:], inorder[k + 1:])
     return root
        def helper(root: TreeNode, val: int) -> TreeNode:
            if not root:
                return TreeNode(val)

            if val > root.val:
                root.right = helper(root.right, val)
            else:
                root.left = helper(root.left, val)
            return root
 def helper(root: TreeNode) -> TreeNode:
     if root:
         helper(root.left)
         helper(root.right)
         if root:
             if root.left and root.left.val == 0 and not root.left.left and not root.left.right:
                 root.left = None
             if root.right and root.right.val == 0 and not root.right.right and not root.right.left:
                 root.right = None
 def helper(nums: List[int], start: int, end: int):
     if start <= end:
         value = max(nums[start:end + 1])
         index = nums.index(value)
         node = TreeNode(value)
         node.left = helper(nums, start, index - 1)
         node.right = helper(nums, index + 1, end)
         return node
     else:
         return None
Exemple #9
0
    def removeLeafNodes(self, root: TreeNode, target: int) -> TreeNode:
        if not root:
            return None

        root.left = self.removeLeafNodes(root.left, target)
        root.right = self.removeLeafNodes(root.right, target)
        if not root.left and not root.right and root.val == target:
            return None
        else:
            return root
Exemple #10
0
 def flatten(self, root: TreeNode) -> None:
     """
     Do not return anything, modify root in-place instead.
     """
     if not root:
         return None
     self.flatten(root.right)
     self.flatten(root.left)
     root.right = self.pre
     root.left = None
     self.pre = root
 def buildTree(self, inorder: List[int], postorder: List[int]) -> TreeNode:
     if not postorder:
         return None
     # 从后续遍历中获取根节点
     v = postorder[-1]
     # 从中序遍历中获取左右子树的分割点
     n = inorder.index(v)
     # 创建根节点
     root = TreeNode(v)
     # 根据n来构建二叉树进行分割递归
     root.left = self.buildTree(inorder[:n], postorder[:n])
     root.right = self.buildTree(inorder[n + 1:], postorder[n:-1])
     return root
    def increasingBST(self, root: TreeNode) -> TreeNode:
        res = []

        def helper(root: TreeNode) -> List[int]:
            if not root:
                return
            helper(root.left)
            res.append(root.val)
            helper(root.right)
            return res

        helper(root)
        current = TreeNode(res[0])
        root = current
        for index, value in enumerate(res):
            if index + 1 >= len(res):
                break
            current.right = TreeNode(res[index + 1])
            current = current.right

        return root
    def bstFromPreorder(self, preorder: List[int]) -> TreeNode:
        def helper(root: TreeNode, val: int) -> TreeNode:
            if not root:
                return TreeNode(val)

            if val > root.val:
                root.right = helper(root.right, val)
            else:
                root.left = helper(root.left, val)
            return root

        root = TreeNode(preorder[0])
        for v in preorder[1:]:
            helper(root, v)
        return root