Exemple #1
0
    def test(root):
        print()
        print("#" * 80)
        print_tree(root)

        #maxlen = maxlen_unival_path(root)
        maxlen = maxlen_unival_path1b(root)

        print(f"\nmax length of any unival path = {maxlen}")
    def test(root):
        print()
        print("#"*80)
        print_tree(root)

        diam = diameter_bt(root)
        diam2 = diameter_bt2(root)

        print(f"\ndiameter of BT (sol #1) = {diam}")
        print(f"diameter of BT (sol #2) = {diam2}")
Exemple #3
0
    def test(arr):
        head, _ = build_ll(arr)

        #root = build_bst(head)
        root = build_bst2(head)

        print("#" * 80)
        print(arr)
        print()
        print_tree(root)
Exemple #4
0
    def test(t1, t2, comment=None):
        t1 = array_to_bt_lc(t1)
        t2 = array_to_bt_lc(t2)

        root = sol.mergeTrees(t1, t2)

        print("=" * 80)
        if comment:
            print(comment, "\n")

        print_tree(root)
Exemple #5
0
    def test(arr):
        root = array_to_bt_lc(arr)

        solutions = [Solution(), Solution2(), Solution3()]

        max_diff = [s.maxAncestorDiff(root) for s in solutions]

        print("#" * 80)
        print_tree(root)

        print(f"\nmax diff (all solutions) = {max_diff}\n")
    def test(s, comment=None):
        print("=" * 80)
        if comment:
            print(comment)

        print(f"\nstring = {s}")

        res = sol.str2tree(s)

        print()
        print_tree(res)
        print()
Exemple #7
0
    def test(arr, comment=None):
        print("=" * 80)
        if comment:
            print(comment)

        root = array_to_bt_lc(arr)

        print(f"\narr = {arr}\n")
        print_tree(root)

        res = sol.maxProduct(root)

        print(f"\nres = {res}\n")
    def test(m, n, comment=None):
        print("=" * 80)
        if comment:
            print(comment)

        root = array_to_bt_lc(arr)

        print()
        print_tree(root)

        res = sol.zigzagLevelOrder(root)

        print(f"\nres = {res}\n")
    def test(arr):
        root = array_to_bt_lc(arr)
        solutions = [Solution(), Solution1b(), Solution2(), Solution3(), 
            Solution4(), Solution5()]

        lcas = [s.lcaDeepestLeaves(root) for s in solutions]
        lca_vals = [lca.val for lca in lcas]

        print("#"*80)
        print(arr)
        print()
        print_tree(root)
        print(f"\nLCA of deepest leaves (all solutions) = {lca_vals}\n")
    def test(arr, comment=None):
        root = array_to_bt_lc(arr)

        solutions = [Solution(), Solution2()]

        res = [s.rightSideView(root) for s in solutions]

        print("=" * 80)
        if comment:
            print(comment, "\n")
        print(arr, "\n")

        print_tree(root)
        print(f"\nSolutions: {res}")
    def test(arr, k, comment=None):
        print("=" * 80)
        if comment:
            print(comment, "\n")

        root = array_to_bt_lc(arr)

        print(arr)
        print(f"k = {k}\n")
        print_tree(root)

        res = sol.kthSmallest(root, k)

        print(f"\nresult: {res}\n")
Exemple #12
0
    def test(arr, comment):
        root = array_to_bt_lc(arr)

        print("=" * 80)
        if comment:
            print(comment)

        print()
        print(arr)
        print()
        print_tree(root)

        res = sol.isSymmetric(root)

        print(f"\nres = {res}\n")
Exemple #13
0
    def test(arr, x, y, comment=None):
        root = array_to_bt_lc(arr)

        solutions = [Solution(), Solution1b(), Solution1c(), Solution2()]

        res = [s.isCousins(root, x, y) for s in solutions]

        print("=" * 80)
        if comment:
            print(comment, "\n")
        print(arr, "\n")

        print_tree(root)

        print(f"\nnodes: {x}, {y}\n")
        print(f"Are cousins (all solutions)? {res}\n")
    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")
Exemple #15
0
 def test_print_tree(self):
     """
     An unit test to test the printing method for a binary tree.
     """
     key_list, value_list = bt.print_tree(self.root)
     self.assertEqual(key_list, [6, 7, 8, 9, 3, 4, 5, 2])
     self.assertEqual(value_list,
                      ['Root', 'A', 'B', 'C', 'D', 'F', 'G', 'E'])
    def test(arr, comment=None):
        root = array_to_bt_lc(arr)

        solutions = [Solution(), Solution2(), Solution3()]

        res = [s.connect(copy.deepcopy(root)) for s in solutions]

        print("=" * 80)
        if comment:
            print(comment, "\n")
        print(arr, "\n")

        print_tree(root)
        print("\nSolutions:")

        for root in res:
            print_levels(root)
Exemple #17
0
    def test(root, p, q):
        solutions = [
            Solution(),
            Solution1b(),
            Solution2(),
            Solution2b(),
            Solution3()
        ]

        lca = [s.lowestCommonAncestor(root, p, q).val for s in solutions]

        print("#" * 80)
        print_tree(root)

        print(f"\np = {p.val}")
        print(f"q = {q.val}")

        print(f"\nLCA (all solutions) = {lca}\n")
Exemple #18
0
    def test(root, target_sum):
        print("\n" + "#" * 80)
        print_tree(root)

        print(f"\ntarget sum = {target_sum}")

        n = num_path_sum(root, target_sum)
        n2 = num_path_sum2(root, target_sum)
        n3 = num_path_sum3(root, target_sum)
        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}")
Exemple #19
0
        inorder(node.left)
        counts[node.val] += 1
        inorder(node.right)

    if not root:
        return []

    counts = collections.defaultdict(int)
    inorder(root)

    # find all keys with max value in the dict counts
    max_val = max(counts.values())

    return [k for k, v in counts.items() if v == max_val]


###############################################################################

if __name__ == "__main__":
    root = TreeNode(1)
    root.right = TreeNode(2)
    root.right.left = TreeNode(2)

    print_tree(root)

    modes = find_mode(root)
    modes2 = find_mode2(root)

    print(f"\nmodes (sol #1) = {modes}")
    print(f"modes (sol #2) = {modes2}")