Пример #1
0
def one_child_asymmetric_returns_ok(sol: Solution, ) -> bool:
    a_root = TreeNode(53, left=TreeNode(57))
    if sol.is_symmetric(a_root):
        return False
    a_root = TreeNode(3, right=TreeNode(7))
    if sol.is_symmetric(a_root):
        return False
    return True
Пример #2
0
def testIsPalindrome():
    assert Solution().isPalindrome([1, 2, 3, 4, 5, 4, 3, 2, 1]) == True
    assert Solution().isPalindrome([1, 2, 3, 4, 5, 5, 3, 2, 1]) == False
    assert Solution().isPalindrome([]) == True
    assert Solution().isPalindrome([1]) == True
    assert Solution().isPalindrome([1, 2]) == False
    assert Solution().isPalindrome([1, 2, 1]) == True
    assert Solution().isPalindrome([1, 1]) == True
    assert Solution().isPalindrome([1, 2]) == False
    assert Solution().isPalindrome([1, 2, 2, 1]) == True
    assert Solution().isPalindrome([1, 2, 2, 2]) == False
Пример #3
0
def asymmetric_at_level_3_tree_returns_ok(sol: Solution, ) -> bool:
    node_vals = (
        4,
        6,
        6,
        8,
        10,
        10,
        8,
        16,
        18,
        18,
        16,
    )
    nodes = tuple(TreeNode(i) for i in node_vals)
    nodes[0].left = nodes[1]
    nodes[0].right = nodes[2]
    nodes[1].left = nodes[3]
    nodes[1].right = nodes[4]
    nodes[2].left = nodes[5]
    nodes[2].right = nodes[6]
    nodes[4].left = nodes[7]
    nodes[4].right = nodes[8]
    nodes[6].left = nodes[9]
    nodes[6].right = nodes[-1]
    return not sol.is_symmetric(nodes[0])
Пример #4
0
def maximally_imbalanced_tree_height_4_returns_ok(sol: Solution, ) -> bool:
    nodes = tuple(TreeNode(i) for i in (
        17,
        19,
        23,
        29,
        31,
    ))
    for i in range(len(nodes) - 1):
        nodes[i].left = nodes[i + 1]
    return not sol.is_symmetric(nodes[0])
Пример #5
0
def asymmetric_tree_sorted_from_1_returns_ok(sol: Solution, ) -> bool:
    nodes = tuple(TreeNode(i) for i in (
        1,
        2,
        2,
        3,
        3,
    ))
    nodes[0].left = nodes[1]
    nodes[0].right = nodes[2]
    nodes[1].right = nodes[3]
    nodes[2].right = nodes[-1]
    return not sol.is_symmetric(nodes[0])
Пример #6
0
def symmetric_tree_height_4_returns_ok(sol: Solution, ) -> bool:
    nodes = tuple(TreeNode(i) for i in (
        17,
        19,
        19,
        23,
        23,
        29,
        29,
    ))
    nodes[0].left = nodes[1]
    nodes[0].right = nodes[2]
    nodes[1].left = nodes[3]
    nodes[2].right = nodes[4]
    nodes[3].right = nodes[5]
    nodes[4].left = nodes[6]
    return sol.is_symmetric(nodes[0])
Пример #7
0
def testSymmetricTree():
    root = TreeNode(1)
    root.left = TreeNode(2)
    root.left.left = TreeNode(3)
    root.left.right = TreeNode(4)

    root.right = TreeNode(2)
    root.right.left = TreeNode(4)
    root.right.right = TreeNode(3)

    result = []

    def resfn(x, y):
        result.append(x)
        return True

    Solution().bfs(root, resfn)
    assert result == [1, 2, 2, 3, 4, 4, 3]

    assert 1 == 1
    pass
Пример #8
0
def sol() -> Solution:
    return Solution()
Пример #9
0
def height2_asymmetric_returns_ok(sol: Solution, ) -> bool:
    a_root = TreeNode(53, left=TreeNode(57))
    a_root.right = TreeNode(57)
    a_root.right.right = TreeNode(13)
    return not sol.is_symmetric(a_root)
Пример #10
0
def island_root_returns_ok(sol: Solution, ) -> bool:
    return sol.is_symmetric(TreeNode(53))