Ejemplo n.º 1
0
def test():
    null = None
    assert Solution().isBalanced(root=build_tree_node([]))
    assert not Solution().isBalanced(
        root=build_tree_node([1, 2, 2, 3, 3, null, null, 4, 4]))
    assert Solution().isBalanced(
        root=build_tree_node([3, 9, 20, null, null, 15, 7]))
Ejemplo n.º 2
0
def test():
    null = None
    assert not Solution().hasPathSum(build_tree_node([]), 5)
    assert Solution().hasPathSum(build_tree_node([5]), 5)
    assert not Solution().hasPathSum(build_tree_node([4]), 5)
    assert Solution().hasPathSum(build_tree_node(
        [5, 4, 8, 11, null, 13, 4, 7, 2, null, null, null, 1]),
                                 targetSum=22)
Ejemplo n.º 3
0
def test():
    null = None
    assert Solution().isValidBST(root=tree_node.build_tree_node([2, 1, 3]))
    assert not Solution().isValidBST(
        root=tree_node.build_tree_node([5, 1, 4, null, null, 3, 6]))
    assert not Solution().isValidBST(root=tree_node.build_tree_node([2, 2, 2]))
    assert not Solution().isValidBST(
        root=tree_node.build_tree_node([5, 4, 6, null, null, 3, 7]))
Ejemplo n.º 4
0
def test():
    null = None
    assert Solution().findTarget(tree_node.build_tree_node(
        [5, 3, 6, 2, 4, null, 7]),
                                 k=9)
    assert not Solution().findTarget(
        tree_node.build_tree_node([5, 3, 6, 2, 4, null, 7]), k=28)
    assert Solution().findTarget(tree_node.build_tree_node([2, 1, 3]), k=4)
    assert not Solution().findTarget(tree_node.build_tree_node([1]), k=2)
Ejemplo n.º 5
0
def test():
    null = None
    assert Solution().pathSum(build_tree_node([0, 1, 1]), 1) == 4
    assert Solution().pathSum(
        root=build_tree_node([10, 5, -3, 3, 2, null, 11, 3, -2, null, 1]),
        targetSum=8) == 3
    assert Solution().pathSum(
        root=build_tree_node([5, 4, 8, 11, null, 13, 4, 7, 2, null, null, 5, 11]),
        targetSum=22) == 3
Ejemplo n.º 6
0
def test():
    null = None
    assert Solution().bstFromPreorder(preorder=[4, 2]) == build_tree_node(
        [4, 2])
    assert Solution().bstFromPreorder(
        preorder=[8, 5, 1, 7, 10, 12]) == build_tree_node(
            [8, 5, 10, 1, 7, null, 12])
    assert Solution().bstFromPreorder(preorder=[1, 3]) == build_tree_node(
        [1, null, 3])
Ejemplo n.º 7
0
def test():
    null = None
    assert Solution().zigzagLevelOrder(
        build_tree_node([1, 2, 3, 4, null, null, 5])) == [[1], [3, 2], [4, 5]]
    assert Solution().zigzagLevelOrder(
        build_tree_node([3, 9, 20, null, null, 15, 7])) == [[3], [20, 9],
                                                            [15, 7]]
    assert Solution().zigzagLevelOrder(build_tree_node([1])) == [[1]]
    assert Solution().zigzagLevelOrder(build_tree_node([])) == []
def test():
    null = None
    assert Solution().maxProduct(tree_node.build_tree_node([1, 2, 3, 4, 5,
                                                            6])) == 110
    assert Solution().maxProduct(
        tree_node.build_tree_node([1, null, 2, 3, 4, null, null, 5, 6])) == 90
    assert Solution().maxProduct(
        tree_node.build_tree_node([2, 3, 9, 10, 7, 8, 6, 5, 4, 11, 1])) == 1025
    assert Solution().maxProduct(tree_node.build_tree_node([1, 1])) == 1
Ejemplo n.º 9
0
def test():
    null = None
    assert not Solution().isCousins(
        root=build_tree_node([1, 2, 3, null, 4]), x=2, y=3)
    assert not Solution().isCousins(
        root=build_tree_node([1, 2, 3, 4]), x=4, y=3)
    assert Solution().isCousins(root=build_tree_node(
        [1, 2, 3, null, 4, null, 5]),
                                x=5,
                                y=4)
Ejemplo n.º 10
0
def test():
    assert Solution().invertTree(root=build_tree_node([])) == build_tree_node(
        [])
    assert Solution().invertTree(root=build_tree_node([1])) == build_tree_node(
        [1])
    assert Solution().invertTree(
        root=build_tree_node([1, 2])) == build_tree_node([1, None, 2])
    assert Solution().invertTree(
        root=build_tree_node([1, 2, 3])) == build_tree_node([1, 3, 2])
    assert Solution().invertTree(
        root=build_tree_node([4, 2, 7, 1, 3, 6, 9])) == build_tree_node(
            [4, 7, 2, 9, 6, 3, 1])
Ejemplo n.º 11
0
def test():
    assert Solution().diameterOfBinaryTree(
        root=build_tree_node([1, 2, 3, 4, 5])) == 3
    assert Solution().diameterOfBinaryTree(root=build_tree_node([1, 2])) == 1
    l = [x for x in range(10000)]
    assert Solution().diameterOfBinaryTree(root=build_tree_node(l)) == 25
    null = None
    l = [
        4, -7, -3, null, null, -9, -3, 9, -7, -4, null, 6, null, -6, -6, null,
        null, 0, 6, 5, null, 9, null, null, -1, -4, null, null, null, -2
    ]
    assert Solution().diameterOfBinaryTree(root=build_tree_node(l)) == 8
Ejemplo n.º 12
0
def test():
    assert Solution().pathSum(build_tree_node([0, 1, 1]), 1) == [[0, 1], [0, 1]]
    root1 = build_tree_node([5, 4, 8, 11, null, 13, 4, 7, 2, null, null, 5, 1])
    answer = [[5, 4, 11, 2], [5, 8, 4, 5]]
    ret = Solution().pathSum(root1, 22)
    assert len(answer) == len(ret)
    for r in ret:
        assert r in answer

    assert Solution().pathSum(build_tree_node([1, 2, 3]), 5) == []
    assert Solution().pathSum(build_tree_node([1, 2]), 0) == []
    assert Solution().pathSum(build_tree_node([]), 1) == []
Ejemplo n.º 13
0
def test():
    null = None
    ans = build_tree_node([3, 9, 20, null, null, 15, 7])
    assert Solution().buildTree(preorder=[3, 9, 20, 15, 7],
                                inorder=[9, 3, 15, 20, 7]) == ans

    ans = build_tree_node([-1])
    assert Solution().buildTree(preorder=[-1], inorder=[-1]) == ans

    ans = build_tree_node([1, null, 2])
    assert Solution().buildTree(preorder=[1, 2], inorder=[1, 2]) == ans

    ans = build_tree_node([1, 2])
    assert Solution().buildTree(preorder=[1, 2], inorder=[2, 1]) == ans
Ejemplo n.º 14
0
def test():
    null = None
    assert Solution().kthSmallest(root=tree_node.build_tree_node(
        [3, 1, 4, null, 2]),
                                  k=1) == 1
    assert Solution().kthSmallest(root=tree_node.build_tree_node(
        [5, 3, 6, 2, 4, null, null, 1]),
                                  k=3) == 3

    assert Solution().kthSmallest2(root=tree_node.build_tree_node(
        [3, 1, 4, null, 2]),
                                   k=1) == 1
    assert Solution().kthSmallest2(root=tree_node.build_tree_node(
        [5, 3, 6, 2, 4, null, null, 1]),
                                   k=3) == 3
Ejemplo n.º 15
0
def test():
    assert Solution().countNodes2(root=build_tree_node([])) == 0
    assert Solution().countNodes2(root=build_tree_node([1])) == 1
    assert Solution().countNodes2(root=build_tree_node([1, 2])) == 2
    assert Solution().countNodes2(root=build_tree_node([1, 2, 3])) == 3
    assert Solution().countNodes2(root=build_tree_node([1, 2, 3, 4])) == 4
    assert Solution().countNodes2(root=build_tree_node([1, 2, 3, 4, 5])) == 5
    assert Solution().countNodes2(
        root=build_tree_node([1, 2, 3, 4, 5, 6])) == 6
Ejemplo n.º 16
0
def test():
    null = None
    bst_iterator = BSTIterator(build_tree_node([7, 3, 15, null, null, 9, 20]))
    assert bst_iterator.next() == 3
    assert bst_iterator.next() == 7
    assert bst_iterator.hasNext()
    assert bst_iterator.next() == 9
    assert bst_iterator.hasNext()
    assert bst_iterator.next() == 15
    assert bst_iterator.hasNext()
    assert bst_iterator.next() == 20
    assert not bst_iterator.hasNext()
Ejemplo n.º 17
0
def test():
    assert Solution().insertIntoBST(root=build_tree_node([4, 2, 7, 1, 3]), val=5)
    assert Solution().insertIntoBST(root=build_tree_node([40, 20, 60, 10, 30, 50, 70]), val=25)
def test():
    null = None
    assert Solution().minDepth(
        root=build_tree_node([3, 9, 20, null, null, 15, 7])) == 2
    assert Solution().minDepth(
        root=build_tree_node([2, null, 3, null, 4, null, 5, null, 6])) == 5
Ejemplo n.º 19
0
def test():
    assert Solution().searchBST(root=build_tree_node([4, 2, 7, 1, 3]),
                                val=2) == build_tree_node([2, 1, 3])