Exemplo n.º 1
0
        stack.append(p.right)
        stack.append(q.left)

    return True


# Recursive Solution
def isSymmetric_recursively(root):
    if not root:
        return True
    return symmetric(root.left, root.right)


# Time: O(n)
# Space: O(h)
def symmetric(left, right):
    if left is None and right is None:
        return True
    if left is None or right is None or left.val != right.val:
        return False
    return symmetric(left.left, right.right) and symmetric(
        left.right, right.left)


if __name__ == '__main__':
    # root = dt.deserialize('[2,1,3,0,7,9,1,2,null,1,0,null,null,8,8,null,null,null,null,7]')
    root = dt.deserialize('[1,2,2,3,4,4,3]')
    # root = dt.deserialize('[1,2,3]')
    res = isSymmetric(root)
    print(res)
Exemplo n.º 2
0
    #         return True
    #     # left OR right
    #     # A + B == TOTAL => A = TOTAL - B
    #     return hasPathSum_iteratively(root.left, target-root.val) \
    #             or hasPathSum_iteratively(root.right, target - root.val)
    # =============================================================================
    return helper(root, target, 0)


def helper(node, target, total):
    if node is None:
        return False

    total += node.val
    if node.left is None and node.right is None and total == target:
        return True
    left = helper(node.right, target, total)
    right = helper(node.left, target, total)
    return left or right
    # return left or right
    # return helper(node.right, target, total + node.val) or helper(node.left, target, total + node.val)


if __name__ == '__main__':
    # root = dt.deserialize('[2,1,3,0,7,9,1,2,null,1,0,null,null,8,8,null,null,null,null,7]')
    # root = dt.deserialize('[1,2,2,3,4,4,3]')
    # root = dt.deserialize('[5,4,8,11,null,13,4,7,2,null,null,null,1]')
    root = dt.deserialize('[-2,null,-3]')
    res = hasPathSum_iteratively(root, -5)
    print(res)
Exemplo n.º 3
0
                nextLevel.append(node.left)
            if node.right is not None:
                nextLevel.append(node.right)
        res.append(level)
        current = nextLevel

    return res


def levelOrder2(root):
    if not root:
        return []
    visited = []
    level = [root]
    while level:
        visited.append([node.val
                        for node in level])  # expend() will unpack nested list
        level = [
            child for node in level for child in (node.left, node.right)
            if child
        ]
    return visited


if __name__ == '__main__':
    root = dt.deserialize(
        '[2,1,3,0,7,9,1,2,null,1,0,null,null,8,8,null,null,null,null,7]')
    res = levelOrder2(root)
    for i in res:
        print(i)
Exemplo n.º 4
0
#         for node in current:
#             if node.left:
#                 next_level.append(node.left)
#             if node.right:
#                 next_level.append(node.right)
#         current = next_level
# =============================================================================
        current = [child for node in current for child in (node.left, node.right) if child]
    return level

# Time O(n)
# Space O(h) h is the height of the binary tree
def maxDepth_recursively(root):
    if not root:
        return 0
# =============================================================================
#     max_left = maxDepth_recursively (root.left) + 1
#     max_right = maxDepth_recursively(root.right) + 1
#     return max(max_left, max_right)
# =============================================================================
    else:
        return max(maxDepth_recursively(root.left), maxDepth_recursively(root.right)) + 1

if __name__ == '__main__':
    # root = dt.deserialize('[2,1,3,0,7,9,1,2,null,1,0,null,null,8,8,null,null,null,null,7]')
    root = dt.deserialize('[3,9,20,null,null,15,7]')
    res = maxDepth_recursively(root)
    print(res)