Exemple #1
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
Exemple #4
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 #5
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 #6
0
    def bstToGst(self, root: TreeNode) -> TreeNode:
        if root:
            self.bstToGst(root.right)
            self.total += root.val
            root.val = self.total
            self.bstToGst(root.left)

        return root
    def str2tree(self, s: str) -> TreeNode:
        def is_number(ss):
            for cc in ss:
                if not cc.isnumeric() and not cc == "-":
                    return False
            return True

        if not s:
            return None

        lst = []
        for ch in s:
            if is_number(ch):
                if lst and is_number(lst[-1]):
                    lst[-1] += ch
                else:
                    lst.append(ch)
            else:
                lst.append(ch)

        print(lst)

        stack = [TreeNode(int(lst[0]))]
        i = 1
        while i < len(lst):
            if lst[i] == "(":
                new = TreeNode(int(lst[i + 1]))
                if not stack[-1].left:
                    stack[-1].left = new
                    stack.append(new)
                else:
                    stack[-1].right = new
                    stack.append(new)
                i += 2
            elif lst[i] == ")":
                stack.pop()
                i += 1

        return stack[0]
Exemple #8
0
from LeetTool import TreeNode
from LeetTool import build_TreeNode

# Definition for a binary tree node.


class Solution:
    def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode',
                             q: 'TreeNode') -> 'TreeNode':
        pass


if __name__ == "__main__":
    tree = build_TreeNode([3, 5, 1, 6, 2, 0, 8, None, None, 7, 4])
    print(Solution().lowestCommonAncestor(tree, tree.left, tree.right))  # 3

    tree = build_TreeNode([3, 5, 1, 6, 2, 0, 8, None, None, 7, 4])
    print(Solution().lowestCommonAncestor(tree, tree.left,
                                          tree.left.right.right))  # 5

    tree = build_TreeNode([3, 5, 1, 6, 2, 0, 8, None, None, 7, 4])
    node2 = TreeNode(10)
    print(Solution().lowestCommonAncestor(tree, tree.left, node2))  # None