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)
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(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): 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(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")
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, 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")
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, 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)
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)