def test(arr):
        solutions = [
            NotSolution(),
            Solution(),
            Solution1b(),
            Solution2(),
            Solution2b(),
        ]

        root = array_to_bt(arr)[0]
        is_bst = [s.isValidBST(root) for s in solutions]

        print("#"*80)
        print(arr)
        print()
        print_tree(root)
        print(f"\nis BST? {is_bst}\n")
Beispiel #2
0
        n3b = num_path_sum3b(root, target_sum)
        n3c = num_path_sum3c(root, target_sum)
        n4 = num_path_sum4(root, target_sum)
        n5, cache = num_path_sum5(root, target_sum)

        print(f"\nnumber of paths with sum {target_sum} (sol #1) is {n}")
        print(f"number of paths with sum {target_sum} (sol #2) is {n2}")
        print(f"number of paths with sum {target_sum} (sol #3) is {n3}")
        print(f"number of paths with sum {target_sum} (sol #3b) is {n3b}")
        print(f"number of paths with sum {target_sum} (sol #3c) is {n3c}")
        print(f"number of paths with sum {target_sum} (sol #4) is {n4}")
        print(f"number of paths with sum {target_sum} (sol #5) is {n5}")

    # LC example
    arr = [10, 5, -3, 3, 2, None, 11, 3, -2, None, 1]
    root = array_to_bt(arr)[0]
    target_sum = 8
    test(root, target_sum)

    # LC test; answer = 1
    arr = [1, 2, None]
    root = array_to_bt(arr)[0]
    target_sum = 2
    test(root, target_sum)

    ### LC test; answer = 3
    arr = [5, 4, 8, 11, None, 13, 4, 7, 2, None, None, 5, 1]
    root = array_to_bt(arr)[0]
    target_sum = 22
    test(root, target_sum)
        diam2 = diameter_bt2(root)

        print(f"\ndiameter of BT (sol #1) = {diam}")
        print(f"diameter of BT (sol #2) = {diam2}")

    root = None
    test(root)
    
    root = TreeNode(1)
    test(root)

    root = TreeNode(1, TreeNode(2, TreeNode(3, TreeNode(4, TreeNode(5, )))))
    test(root)

    arr = [5, 4,5, 1,1,None,5] 
    nodes = array_to_bt(arr)
    root = nodes[0]
    test(root)

    arr = [1, 4,5, 4,4,None,5] 
    nodes = array_to_bt(arr)
    root = nodes[0]
    test(root)

    arr = [5,4,5,4,4,5,3,4,4,None,None,None,4,None,None,4,None,None,4,None,4,4,None,None,4,4]
    root = array_to_bt_lc(arr)
    test(root)

    arr = [1, 2,3, 4,5] # LC example; answer = 3
    root = array_to_bt_lc(arr)
    test(root)