Exemplo n.º 1
0
def test():
    null = None
    cases = [
        ([1, null, 0, 0, 1], [1, null, 0, null, 1]),
        ([1, 0, 1, 0, 0, 0, 1], [1, null, 1, null, 1]),
        ([1, 1, 0, 1, 1, 0, 1, 0], [1, 1, 0, 1, 1, null, 1]),
    ]
    sol = Solution()
    ok = True
    for c in cases:
        (root, exp) = c
        res = tree_to_list(sol.pruneTree(list_to_tree(root)))
        if res != exp:
            print('case failed. {}, out = {}'.format(c, res))
            ok = False
    if ok:
        print('cases passed!!!')
            if not check(node.right, node.val, maxB, cross):
                return False

            return True

        minB, maxB = 0, 1 << 30
        cross = [0]
        if not check(root, minB, maxB, cross):
            return None
        if cross[0] != len(trees) - 1:
            return None

        # import aatest_helper
        # print(aatest_helper.tree_to_list(root))
        return root


true, false, null = True, False, None
import aatest_helper

cases = [
    # ([aatest_helper.list_to_tree(x) for x in [[2, 1], [3, 2, 5], [5, 4]]], aatest_helper.ANYTHING),
    ([aatest_helper.list_to_tree(x)
      for x in [[5, 3, 8], [3, 2, 6]]], aatest_helper.ANYTHING),
]

aatest_helper.run_test_cases(Solution().canMerge, cases)

if __name__ == '__main__':
    pass
Exemplo n.º 3
0
            l = walk(root.left)
            r = walk(root.right)
            res = [0] * (k + 1)

            res[0] = max(l) + max(r)

            for i in range(1, k + 1):
                for j in range(i):
                    jj = i - 1 - j
                    if jj >= 0 and jj < k:
                        res[i] = max(res[i], root.val + l[j] + r[jj])

            return res

        res = walk(root)
        return max(res)


null = None
import aatest_helper
cases = [
    (aatest_helper.list_to_tree([4, 1, 3, 9, null, null, 2]), 2, 16),
    (aatest_helper.list_to_tree([5, 2, 3, 4]), 2, 12),
    (aatest_helper.list_to_tree([8, 1, 3, 9, 9, 9, null, 9, 5, 6, 8]), 2, 52),
]

aatest_helper.run_test_cases(Solution().maxValue, cases)

if __name__ == '__main__':
    pass
Exemplo n.º 4
0
                    if root.right is None and y == 1: continue

                    b = test(root.right, y, me)

                    # x, y, me, parent.
                    if x + y + me + parent == 0:
                        continue

                    cost = a + b + me
                    ans = min(ans, cost)

            dp[key] = ans
            return ans

        a = test(root, 0, 0)
        b = test(root, 1, 0)
        return min(a, b)


import aatest_helper

null = None
cases = [
    (aatest_helper.list_to_tree([0, 0, null, 0, 0]), 1),
    (aatest_helper.list_to_tree([0, 0, null, 0, null, 0, null, null, 0]), 2),
]

import aatest_helper

aatest_helper.run_test_cases(Solution().minCameraCover, cases)
Exemplo n.º 5
0
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None


class Solution:
    def bstToGst(self, root: TreeNode) -> TreeNode:
        def fn(root: TreeNode, pv):
            if root is None:
                return pv

            pv = fn(root.right, pv)            
            pv += root.val
            root.val = pv
            pv = fn(root.left, pv)
            return pv

        pv = fn(root, 0)
        return root


import aatest_helper

null = None
t = aatest_helper.list_to_tree(
    [4, 1, 6, 0, 2, 5, 7, null, null, null, 3, null, null, null, 8])

t = Solution().bstToGst(t)
print(aatest_helper.tree_to_list(t))
Exemplo n.º 6
0

class Solution:
    def rangeSumBST(self, root: TreeNode, L: int, R: int) -> int:
        def fn(root):
            if root is None:
                return 0
            ans = 0
            if root.val < L:
                ans += fn(root.right)
            elif root.val > R:
                ans += fn(root.left)
            else:
                ans += root.val
                ans += fn(root.right)
                ans += fn(root.left)
            return ans

        ans = fn(root)
        return ans


null = None
cases = [
    (aatest_helper.list_to_tree([10, 5, 15, 3, 7, null, 18]), 7, 15, 32),
    (aatest_helper.list_to_tree([10, 5, 15, 3, 7, 13, 18, 1, null, 6]), 6, 10, 23)
]

sol = Solution()
aatest_helper.run_test_cases(sol.rangeSumBST, cases)
Exemplo n.º 7
0
        while True:
            root.count += 1
            if root.left is None:
                root.left = node
                break
            elif root.right is None:
                root.right = node
                break

            lc = root.left.count
            rc = root.right.count
            if (lc & (lc + 1)) != 0:
                root = root.left
            elif (rc & (rc + 1)) != 0:
                root = root.right
            else:
                root = root.left if rc == lc else root.right

        return root.val

    def get_root(self) -> TreeNode:
        return self.root


import aatest_helper
null = None
cases = [(["CBTInserter", "insert", "insert",
           "get_root"], [[aatest_helper.list_to_tree([1, 2, 3, 4, 5, 6])], [7],
                         [8], []], [null, 3, 4, [1, 2, 3, 4, 5, 6, 7, 8]])]
aatest_helper.run_simulation_cases(CBTInserter, cases)
Exemplo n.º 8
0
class Solution:
    def maxProduct(self, root: TreeNode) -> int:
        def visit(root: TreeNode):
            if root is None:
                return 0
            a = visit(root.left)
            b = visit(root.right)
            root.sum = a + b + root.val
            return root.sum

        tt = visit(root)

        def cut(root: TreeNode):
            if root is None:
                return 0

            a = cut(root.left)
            b = cut(root.right)
            ans = max(a, b, (tt - root.sum) * root.sum)
            return ans

        ans = cut(root) % (10**9 + 7)
        return ans


import aatest_helper
cases = [(aatest_helper.list_to_tree([1, 2, 3, 4, 5, 6]), 110),
         (aatest_helper.list_to_tree([2, 3, 9, 10, 7, 8, 6, 5, 4, 11,
                                      1]), 1025)]
aatest_helper.run_test_cases(Solution().maxProduct, cases)
Exemplo n.º 9
0
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None


class Solution:
    def rob(self, root: TreeNode) -> int:
        def fn(root: TreeNode):
            # max value of (rob, !rob)
            if root is None:
                return 0, 0

            (x0, y0) = fn(root.left)
            (x1, y1) = fn(root.right)
            a = root.val + y0 + y1
            b = max(x0, y0) + max(x1, y1)
            return a, b

        a, b = fn(root)
        ans = max(a, b)
        return ans


import aatest_helper

null = None
cases = [(aatest_helper.list_to_tree([3, 2, 3, null, 3, null, 1]), 7)]

aatest_helper.run_test_cases(Solution().rob, cases)