def test_impl(func: SolutionFunc, nodes: NodeList, expected: NodeList) -> None: root = bst.build_tree(nodes) func(root) r = bst.build_list(root) e = bst.build_list(bst.build_tree(expected)) if r == e: print( colored(f"PASSED {func.__name__} => {nodes} flattened to {r}", "green")) else: print( colored( f"FAILED {func.__name__} => {nodes} flattened to {r} but expected {expected}", "red", ))
def test_impl( func: SolutionFunc, tree_nodes: List[Optional[int]], p: int, q: int, expected: int, ) -> None: root = bst.build_tree(tree_nodes) assert root, "root must not be None!" p_node = find_node(root, p) q_node = find_node(root, q) assert p_node and q_node, f"{p} and {q} must both be found in root!" r = func(root, p_node, q_node) if r and r.val == expected: print( colored( f"PASSED {func.__name__} => Lowest common ancestor in {tree_nodes} for {p} and {q} is {r}", "green", )) else: print( colored( f"FAILED {func.__name__} => Lowest common ancestor in {tree_nodes} for {p} and {q} is {r} but expected {expected}", "red", ))
def test_impl(func: SolutionFunc, nodes: List[Any], expected: List[Any]): root = bst.build_tree(nodes) r = func(root) r_nodes = bst.build_list(r) e_nodes = bst.build_list(bst.build_tree(expected)) if r_nodes == e_nodes: print( colored( f"PASSED {func.__name__} => bst:{nodes} to greater tree is: {r_nodes}", "green", )) else: print( colored( f"FAILED {func.__name__} => bst:{nodes} to greater tree is: {r_nodes}, expected: {e_nodes}", "red", ))
def test_impl(func: SolutionFunc, nodes: NodeList, v: int, d: int, expected: NodeList) -> None: root = build_tree(nodes) assert root is not None, f"root cannot be None." r = func(root, v, d) r_nodes = build_list(r) if r_nodes == build_list(build_tree(expected)): print( colored( f"PASSED {func.__name__} => {nodes} with v={v} and d={d} is {r_nodes}", "green", )) else: print( colored( f"FAILED {func.__name__} => {nodes} with v={v} and d={d} is {r_nodes} but expected {expected}", "red", ))
def test_impl( func: SolutionFunc, nodes: List[Optional[int]], low: int, high: int, expected: List[Optional[int]], ) -> Optional[TreeNode]: root = bst.build_tree(nodes) root = func(root, low, high) r = bst.build_list(root) if r == bst.build_list(bst.build_tree(expected)): print( colored( f"PASSED {func.__name__}=> binary tree of {nodes} with range min={low}, max={high} trimmed is {r}", "green", )) else: print( colored( f"FAILED {func.__name__}=> binary tree of {nodes} with range min={low}, max={high} trimmed is {r}, expected; {expected}", "red", ))
def test_impl(func: SolutionFunc, nodes: NodeList, expected: int) -> None: root = bst.build_tree(nodes) r = func(root) if r == expected: print( colored( f"PASSED {func.__name__} => Min cameras to place in {nodes} is {r}", "green", )) else: print( colored( f"FAILED {func.__name__} => Min cameras to place in {nodes} is {r} but expected {expected}", "red", ))
def test_impl(func: SolutionFunc, nodes: List[Optional[int]], expected: List[float]) -> None: root = build_tree(nodes) r = func(root) if r == expected: print( colored( f"PASSED {func.__name__} => {nodes} average level values are {r}", "green", )) else: print( colored( f"FAILED {func.__name__} => {nodes} average level values are {r}, but expected {expected}", "red", ))
def test_impl(func: SolutionFunc, nodes: List[Optional[int]], expected: List[int]): root = bst.build_tree(nodes) r = func(root) if r == expected: print( colored( f"PASS {func.__name__} => tree: {nodes} right side view: {r}", "green", )) else: print( colored( f"FAILED {func.__name__} => tree: {nodes} right side view: {r}, but expected: {expected}", "red", ))
def test_impl(func: SolutionFunc, nums: List[int], possible_results: PossibleResults) -> None: res = func(nums) res_list = bst.build_list(res) if any(res_list == bst.build_list(bst.build_tree(exp)) for exp in possible_results): print( colored( f"PASSED {func.__name__} => Sorted array {nums} to height-balanced BST is {res_list}", "green", )) else: print( colored( f"FAILED {func.__name__} => Sorted array {nums} to height-balanced BST is {res_list} but expected any from {possible_results}", "red", ))
def test_impl( func: SolutionFunc, nodes: NodeList, expected: List[List[int]] ) -> None: root = bst.build_tree(nodes) r = func(root) if sorted(r) == sorted(expected): print( colored( f"PASSED {func.__name__} => Level ordered traversal of {nodes} is {r}", "green", ) ) else: print( colored( f"FAILED {func.__name__} => Level ordered traversal of {nodes} is {r} but expected {expected}", "red", ) )
def test_impl( func: SolutionFunc, preorder: List[int], inorder: List[int], expected: List[Optional[int]], ) -> None: r = func(preorder, inorder) r_list = bst.build_list(r) e_list = bst.build_list(bst.build_tree(expected)) if r_list == e_list: print( colored( f"PASSED {func.__name__} => BST from preorder: {preorder} and inorder: {inorder} is {r}", "green", )) else: print( colored( f"FAILED {func.__name__} => BST from preorder: {preorder} and inorder: {inorder} is {r} but expected {expected}", "red", ))
def test_impl( func: SolutionFunc, nodes: ListNodeDataList, expected: TreeNodeDataList ) -> None: head = linked_list.build_linked_list(nodes) root = func(head) bst_nodes = bst.build_list(root) exp_nodes = bst.build_list(bst.build_tree(expected)) if bst_nodes == exp_nodes: print( colored( f"PASSED {func.__name__} => {nodes} converted to BST: {bst_nodes}", "green", ) ) else: print( colored( f"FAILED {func.__name__} => {nodes} converted to BST: {bst_nodes} but expected {expected}", "red", ) )
def test_solution(nodes: List[Optional[int]], expected: List[List[int]]): root = bst.build_tree(nodes) sln = Solution() r = sln.verticalTraversal(root) success = len(r) == len(expected) if success: for i in range(0, len(r)): l1 = r[i] l2 = expected[i] if l1 != l2: success = False break if success: print( colored(f"PASSED => vertical travesal of {nodes} is {r}", "green")) else: print( colored( f"FAILED => vertical travesal of {nodes} is {r}, but expected: {expected}", "red", ))
def test_impl(func: SolutionFunc, nodes: NodeList, p_val: int, q_val: int, expected: int) -> None: root = bst.build_tree(nodes) assert root, "root cannot be None" p = find_node(root, p_val) assert p, "p cannot be None" q = find_node(root, q_val) assert q, "q cannot be None" res = func(root, p, q) assert res, "res cannot be None" if res.val == expected: print( colored( f"PASSED {func.__name__} => Lowest common ancestor of {p_val} and {q_val} in tree {nodes} is {res.val}", "green", )) else: print( colored( f"FAILED {func.__name__} => Lowest common ancestor of {p_val} and {q_val} in tree {nodes} is {res.val} but expected {expected}", "red", ))