Exemple #1
0
    def splitBST(self, root: TreeNode, V: int) -> List[TreeNode]:
        if not root:
            return [None, None]

        if V < root.val:
            left, right = self.splitBST(root.left, V)
            root.left = right
            return [left, root]
        elif root.val < V:
            left, right = self.splitBST(root.right, V)
            root.right = left
            return [root, right]
        else:
            right = root.right
            root.right = None
            return [root, right]
Exemple #2
0
    def convertBiNode(self, root: TreeNode, bigger=None) -> TreeNode:
        if root:
            # 处理当前节点
            left, right = root.left, root.right
            root.left = None

            # 处理当前节点的右侧节点
            if right:
                root.right = self.convertBiNode(right, bigger)
            else:
                root.right = bigger

            if left:
                return self.convertBiNode(left, root)
            else:
                return root
Exemple #3
0
 def dfs(self, lst, l, r):
     if l > r:
         return None
     if l == r:
         return TreeNode(lst[l])
     else:
         m = (l + r) // 2
         root = TreeNode(lst[m])
         root.left = self.dfs(lst, l, m - 1)
         root.right = self.dfs(lst, m + 1, r)
         return root
    def upsideDownBinaryTree(self, root: TreeNode) -> TreeNode:
        if not root:
            return None

        ans = TreeNode(root.val)
        while root.left:
            node = TreeNode(root.left.val)
            node.right = ans
            node.left = root.right
            ans = node
            root = root.left
        return ans
    def encode(self, root: 'Node') -> TreeNode:
        # 处理空树的情况
        if not root:
            return None

        # 递归处理
        head = TreeNode(root.val)
        head.right = TreeNode(None)
        p = head.right
        if root.children:
            for c in root.children:
                p.left = self.encode(c)
                p = p.left
        return head