def has_path_sum(root: TreeNode, target_sum: int) -> bool: """ :param root: root node of Binary Tree :param target_sum: target sum for path to add up to :return: whether the tree has a root-to-leaf path that add up to target sum """ if not root: return False stack = [(root, target_sum)] while stack: current_node, sum_left = stack.pop() if not current_node.left and not current_node.right and current_node.val == sum_left: return True if current_node.left: stack.append((current_node.left, sum_left - current_node.val)) if current_node.right: stack.append((current_node.right, sum_left - current_node.val)) return False test_cases = [ ([5, 4, 8, 11, None, 13, 4, 7, 2, None, None, None, 1], 0, False), ([5, 4, 8, 11, None, 13, 4, 7, 2, None, None, None, 1], 22, True), ] for test_tree, test_target, expected_output in test_cases: assert has_path_sum( ConstructTree.build_tree_leetcode(test_tree).root, test_target) is expected_output
if left_lower > left_upper or left_upper >= current_node.val: return _invalid_bst_lower, _invalid_bst_upper else: left_lower = current_node.val if current_node.right: right_lower, right_upper = BST_checker(current_node.right) # Right tree contains nodes with keys strictly greater than node's key if right_lower > right_upper or right_lower <= current_node.val: return _invalid_bst_lower, _invalid_bst_upper else: right_upper = current_node.val return left_lower, right_upper if not root: return True tree_lower, tree_upper = BST_checker(root) return tree_lower <= tree_upper test_cases = [ ([1, 1], False), ([1, 0], True), ([1, None, 2], True), ([1, None, 1], False), ] for test_tree, expected_output in test_cases: assert is_valid_BST(root=ConstructTree.build_tree_leetcode( test_tree).root) is expected_output
1785, 1789, 1793, 1797, 1801, 1805, 1809, 1813, 1817, 1821, 1825, 1829, None, 1835, 1839, 1843, 1847, 1851, 1855, 1859, 1863, 1867, 1871, 1875, 1879, 1883, 1887, 1891, None, 1897, 1901, 1905, 1909, 1913, 1917, 1921, 1925, 1929, 1933, 1937, 1941, 1945, 1949, 1953, 1957, 1961, 1965, 1969, 1973, 1977, 1981, 1985, 1989, 1993, 1997, 2001, 2005, 2009, 2013, 2017, None, 2023, 2027, 2031, 2035, 2039, 2043, 2047, 2051, 2055, 2059, 2063, 2067, 2071, 2075, 2079, None, 2085, 2089, 2093, 2097, 2101, 2105, 2109, 2113, 2117, 2121, 2125, 2129, 2133, 2137, 2141, None, 2147, 2151, 2155, 2159, 2163, 2167, 2171, 2175, 2179, 2183, 2187, 2191, 2195, 2199, 2203, 2207, 2211, 2215, 2219, 2223, 2227, 2231, 2235, 2239, 2243, 2247, 2251, 2255, 2259, 2263, 2267, None, 2273, 2277, 2281, 2285, 2289, 2293, 2297, 2301, 2305, 2309, 2313, 2317, 2321, 2325, 2329, None, 2335, 2339, 2343, 2347, 2351, 2355, 2359, 2363, 2367, 2371, 2375, 2379, 2383, 2387, 2391, None, 2397, 2401, 2405, 2409, 2413, 2417, 2421, 2425, 2429, 2433, 2437, 2441, 2445, 2449, 2453, 2457, 2461, 2465, 2469, 2473, 2477, 2481, 2485, 2489, 2493, 2497, 2501, 2505, 2509, 2513, 2517, None, 2523, 2527, 2531, 2535, 2539, 2543, 2547, 2551, 2555, 2559, 2563, 2567, 2571, 2575, 2579, None, 2585, 2589, 2593, 2597, 2601, 2605, 2609, 2613, 2617, 2621, 2625, 2629, 2633, 2637, 2641, None, 2647, 2651, 2655, 2659, 2663, 2667, 2671, 2675, 2679, 2683, 2687, 2691, 2695, 2699, 2703, 2707, 2711, 2715, 2719, 2723, 2727, 2731, 2735, 2739, 2743, 2747, 2751, 2755, 2759, 2763, 2767, None, 2773, 2777, 2781, 2785, 2789, 2793, 2797, 2801, 2805, 2809, 2813, 2817, 2821, 2825, 2829, None, 2835, 2839, 2843, 2847, 2851, 2855, 2859, 2863, 2867, 2871, 2875, 2879, 2883, 2887, 2891 ], 2791, 2851, 87451), ] for test_tree_list, test_low, test_high, expected_output in test_cases: assert BST(ConstructTree.build_tree_leetcode( test_tree_list).root).range_accumulate(test_low, test_high) == expected_output
return 0 current_level = deque([(root, 0)]) # (node, node_id) max_width = 0 while current_level: current_level_node_count = len(current_level) start_id = current_id = current_level[0][1] for _ in range(current_level_node_count): current_node, current_id = current_level.popleft() if current_node.left: current_level.append((current_node.left, 2 * current_id)) if current_node.right: current_level.append((current_node.right, 2 * current_id + 1)) end_id = current_id max_width = max(max_width, end_id - start_id + 1) return max_width test_cases = [ ([1, 3, 2, 5, 3, None, 9], 4), ([1, 3, None, 5, 3, None, None], 2), ([1, 3, 2, 5, None, None, None], 2), ([1, 3, 2, 5, None, None, 9, 6, None, None, 7, None, None, None, None], 8) ] for tree_list, expected_out in test_cases: test_tree = ConstructTree.build_tree_leetcode(tree_list) assert width_of_binary_tree(test_tree.root) == expected_out
successor = current_node.right while successor.left is not None and successor.left is not current_node: successor = successor.left if successor.left is None: # no left subtree under successor, make a temporary connection back to current_node # for future traversal successor.left = current_node current_node = current_node.right else: # there is left subtree: this is the temporary connection created in previous path # unlink the path and visit current_node successor.left = None accumulator += current_node.val current_node.val = accumulator current_node = current_node.left return root test_cases = [([4, 1, 6, 0, 2, 5, 7, None, None, None, 3, None, None, None, 8], [30, 36, 21, 36, 35, 26, 15, None, None, None, 33, None, None, None, 8]), ([0, None, 1], [1, None, 1]), ([1, 0, 2], [3, 3, 2]), ([3, 2, 4, 1], [7, 9, 4, 10]), ] for test_input, expected_output in test_cases: for convert_bst in [convert_bst_stack, convert_bst_morris, convert_bst_recurse]: test_tree = ConstructTree.build_tree_leetcode(test_input) test_tree.root = convert_bst(test_tree.root) assert test_tree.leetcode_traversal()[:len(expected_output)] == expected_output, convert_bst.__name__
""" :param root: root node of binary tree :return: sum of left leaf nodes in the binary tree """ if not root or not (root.left or root.right): return 0 sum_left_leaves = 0 # stack keep track of left leaf nodes or non leaf nodes dfs_stack = [root] while dfs_stack: current_node = dfs_stack.pop() if not current_node.left and not current_node.right: sum_left_leaves += current_node.val if current_node.left: dfs_stack.append(current_node.left) if current_node.right and (current_node.right.left or current_node.right.right): # filter out right leaf node dfs_stack.append(current_node.right) return sum_left_leaves test_cases = [ ([3, 9, 20, None, None, 15, 7], 24), ([1], 0), ([1, 2], 2), ] for test_input, expected_output in test_cases: assert sum_of_left_leaves( ConstructTree.build_tree_leetcode(test_input).root) == expected_output
:return: # of paths that end at current_node and add up to target_sum + cumulative results from its descendants """ if not current_node: return 0 root_to_node_sum = root_to_parent_sum + current_node.val root_to_path_start_sum = root_to_node_sum - target_sum # count of paths that end at current_node and add up to target_sum path_count = cache.get(root_to_path_start_sum, 0) cache[root_to_node_sum] = cache.get(root_to_node_sum, 0) + 1 path_count += dfs_traverse(current_node.left, root_to_node_sum, cache) path_count += dfs_traverse(current_node.right, root_to_node_sum, cache) # clear the root -> current_node path before backtracking to a different path cache[root_to_node_sum] -= 1 return path_count path_sum_cache = {0: 1} return dfs_traverse(root, 0, path_sum_cache) test_cases = [ ([10, 5, -3, 3, 2, None, 11, 3, -2, None, 1], 8, 3), ] for test_tree, test_target, expected_count in test_cases: assert path_sum(root=ConstructTree.build_tree_leetcode(test_tree).root, target_sum=test_target) == expected_count, \ test_tree
None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, 2]), ] for test_tree_1, test_tree_2, expected_tree in test_cases: get_tree = BinaryTree(merge_trees((ConstructTree.build_tree_leetcode(test_tree_1).root if test_tree_1 else None), (ConstructTree.build_tree_leetcode(test_tree_2).root if test_tree_2 else None))) get_tree_traversal = get_tree.leetcode_traversal() assert CompareTree.compare_leetcode_traversal(get_tree_traversal, expected_tree)
test_cases = [ ([1, 2, 3, 4, 5], 3), ([1, 2], 1), ([3, 1, None, 4, None, 2, None, 5], 4), ([ -64, 12, 18, -4, -53, None, 76, None, -51, None, None, -93, 3, None, -31, 47, None, 3, 53, -81, 33, 4, None, -51, -44, -60, 11, None, None, None, None, 78, None, -35, -64, 26, -81, -31, 27, 60, 74, None, None, 8, -38, 47, 12, -24, None, -59, -49, -11, -51, 67, None, None, None, None, None, None, None, -67, None, -37, -19, 10, -55, 72, None, None, None, -70, 17, -4, None, None, None, None, None, None, None, 3, 80, 44, -88, -91, None, 48, -90, -30, None, None, 90, -34, 37, None, None, 73, -38, -31, -85, -31, -96, None, None, -18, 67, 34, 72, None, -17, -77, None, 56, -65, -88, -53, None, None, None, -33, 86, None, 81, -42, None, None, 98, -40, 70, -26, 24, None, None, None, None, 92, 72, -27, None, None, None, None, None, None, -67, None, None, None, None, None, None, None, -54, -66, -36, None, -72, None, None, 43, None, None, None, -92, -1, -98, None, None, None, None, None, None, None, 39, -84, None, None, None, None, None, None, None, None, None, None, None, None, None, -93, None, None, None, 98 ], 18), ] for diameter_of_binary_tree in [ diameter_of_binary_tree_local_parameter, diameter_of_binary_tree_global_parameter, ]: for test_tree_list, expected_output in test_cases: assert diameter_of_binary_tree(ConstructTree.build_tree_leetcode(test_tree_list).root) \ == expected_output, diameter_of_binary_tree.__name__
], [10, 8, 16, 4, 2, 22]), ([ 80, 16, 27, 75, 60, 28, 68, 96, 7, None, 62, 45, 58, None, None, None, 1, 54, 15, 26, 65, 70, 50, 37, None, 88, 22, None, 95, 4, 64, None, 33, 47, 85, 13, None, 35, None, 69, None, 74, 78, None, None, None, 73, 67, None, 51, 61, None, 24, 55, 32, 93, 66, 79, None, None, None, None, None, None, 76, None, 8, 90, 82, None, None, None, 39, None, None, None, None, 12, 9, 3, 59, 31, 53, None, 6, None, None, 57, None, None, None, None, None, None, None, None, 87, None, 41, 99, None, None, 29, 43, None, 5, 49, None, 30, None, None, 2, 89, None, None, 84, None, 21, 52, None, None, 100, None, None, 56, 48, 34, 81, 98, None, 97, 40, 25, 77, None, 20, 46, 86, None, None, 14, None, None, None, 36, None, None, None, 23, 10, 19, None, 72, None, None, None, 91, None, None, None, 92, 17, None, None, None, None, None, None, None, 63, 11, None, None, None, 71, None, 94, None, None, 44, 42, None, 83, None, None, None, None, 18, None, None, None, None, None, None, None, None, None, None, 38 ], [ 80, 27, 68, 28, 58, 37, 69, 45, 70, 13, 79, 50, 35, 16, 75, 7, 54, 95, 73, 90, 82, 15, 4, 67, 64, 61, 51, 39, 87, 96, 1, 88, 78, 8, 74, 76, 57, 2, 97, 72, 94, 89, 25, 91, 40, 22, 60, 62, 65, 85, 66, 6, 93, 31, 5, 56, 49, 34, 48, 36, 53, 30, 81, 23, 11, 63, 98, 19, 71, 18, 38, 10, 47, 32, 59, 43, 100, 14, 3, 29, 55, 9, 99, 21, 46, 17, 83, 20, 92, 42, 44, 52, 86, 12, 41, 84, 77, 26, 33, 24 ], [80, 27, 28, 75, 64, 88, 89, 62, 65, 85, 49, 23, 98, 47, 32, 55, 21, 92]), ] for test_input, test_voyage, expected_outcome in test_cases: solution = FlipMatchVoyage() assert solution.flip_match_voyage(ConstructTree.build_tree_leetcode(test_input).root, test_voyage) == \ expected_outcome, test_voyage
-162, 2, -688, None, None, None, None, None, None, None, None, -273, None, None, None, None, 857, -724, None, None, None, None, None, -783, -297, None, -417, None, None, None, -767, None, -368, -478, -394, None, None, -453, None, None, -138, -557, None, None, None, None, None, None, None, None, None, None, 816, 370, None, 870, None, -847, None, None, 356, 756, 849, 325, 206, None, -274, None, None, None, None, -561, -986, -368, None, -891, None, None, -812, None, 316, None, -608, -391, 860, 237, 540, None, None, None, -217, 435, None, None, None, None, None, None, None, None, None, None, 856, -120, 243, None, None, 774, None, None, -975, None, 994, 673, None, None, -218, 704, None, None, -240, None, None, None, None, None, 621, 81, -392, None, None, 898, -351, None, -437, 734, -280, 210, None, None, None, None, None, None, -429, None, -511, 495, -29, -622, 437, 123, None, None, -648, None, None, None, -265, None, None, None, None, 413, None, None, None, None, None, None, 184, -600, None, None, -62, None, None, None, None, 950, 212, 421, None, 897, None, None, None, None, -212, None, None, None, 33, None, -473, 9, -32, -175, -396, -64, None, None, 50, None, None, None, None, 691, 491, -372, None, None, None, None, None, 922, None, None, None, None, None, None, None, None, None, -818, -685, -855, None, -231, 821, -514, None, None, None, None, -657, None, None, None, None, None, None, None, None, None, 268, -564, None, -951, None, None, None, 627, None, None, None, None, None, None, None, 965, None, None, None, None, 1, None, -631, None, None, None, None, None, -679, 106, None, -311, None, None, None, None, None, None, None, None, 735, 397, -813, None, None, None, 757, None, None, None, None, -612, None, None, -756, None, None, None, None, 149, None, None, 306, None, 515, None, -905, 106, None, None, None, -610, -414, -575, None, None, 764, 554, None, 749, -905, None, None, None, -885, None, None, -681, None, None, None, -148, None, -807, None, 713, None, None, 147, None, None, None, None, None, 128, None, None, 201, None, None, None, None, None, 75, None, None, None, None, 377, 176, -489, None, None, None, None, None, None, None, 708, 288, 595, None, None, None, 78, -34, None, None, 942, -903, None, None, None, None, -493, None, None, None, None, None, None, None, None, None, None, None, 286, -132, None, -37, None, None, None, None, None, None, None, None, None, None, None, -129, 894, None, -828, None, None, 455, 909, None, None, -664, -72, None, None, None, None, None, None, None, None, 823, None, -952, None, None, -283, None, -967, None, 774, 149, None, None, None, None, None, 863, 461, 751, None, None, None, None, None, None, None, None, None, None, None, -165, -900], 41), ] for test_tree_list, expected_count in test_cases: assert good_nodes_count(ConstructTree.build_tree_leetcode(test_tree_list).root) == expected_count
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom. """ # Only as placeholder for test cases from _Binary_Tree import ConstructTree test_cases = [ ([1, 2, 3, None, 5, None, 4], [1, 3, 4]), ([1, None, 3], [1, 3]), ([], []), ([ 23, 28, None, 21, None, 6, 19, 48, 8, None, None, None, None, 22, 31, None, None, 4, 36, 34, 30, None, None, None, None, 27, 2, None, None, 3, 26, None, None, 55, None, 1, None, 15, 41, None, None, 37, 39, 14, 51, None, None, None, None, 46, None, 32, 12, None, None, 45, 9, None, None, 29, 10, None, None, None, 18, 43, 25, 47, 50, None, 7, None, None, None, None, 40, 44, 17, None, None, None, 49, None, 35, 20, 11, 54, 53, 5, None, None, 52, 33, None, None, None, 16, None, None, 42, 38, None, None, None, None, 13, 24, None, None, None, None ], [ 23, 28, 21, 19, 8, 31, 36, 30, 2, 26, 55, 1, 41, 39, 51, 46, 12, 9, 10, 18, 25, 7, 44, 17, 49, 20, 5, 16, 38, 24 ]), ] for test_tree, expected_output in test_cases: if expected_output: assert ConstructTree.build_tree_leetcode( test_tree).right_side_view() == expected_output else: assert ConstructTree.build_tree_leetcode(test_tree) is None
None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, 2], True), ] setrecursionlimit(1100) for is_subtree in [is_subtree_merkle_encoding, is_subtree_node_comparison, ]: for tree_1_list, tree_2_list, expected_output in test_cases: assert is_subtree(ConstructTree.build_tree_leetcode(tree_1_list).root, ConstructTree.build_tree_leetcode(tree_2_list).root) is expected_output
""" Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment. Design an algorithm to serialize and deserialize a binary tree. There is no restriction on how your serialization/deserialization algorithm should work. You just need to ensure that a binary tree can be serialized to a string and this string can be deserialized to the original tree structure. Clarification: The input/output format is the same as how LeetCode serializes a binary tree. You do not necessarily need to follow this format, so please be creative and come up with different approaches yourself. """ from _Binary_Tree import ConstructTree test_cases = [[1, 2, 3, None, None, 4, 5], [], [1], [1, 2], ] for test_tree_list in test_cases: old_tree = ConstructTree.build_tree_leetcode(test_tree_list) if old_tree: assert old_tree == ConstructTree.build_tree_leetcode(old_tree.leetcode_traversal())
if found_in_current_node == 2: # same parent return False if x_found or y_found: return x_found and y_found current_level = next_level return False test_cases = [([1, 2, 3, 4], 4, 3, False), ([1, 2, 3, None, 4, None, 5], 5, 4, True), ([1, 2, 3, None, 4], 2, 3, False), ([1, 2, 10, 3, 4, 12, 15, 7, 6, 11, 5, 19, 16, 28, 31, None, 47, 14, 8, None, 17, 25, 13, None, None, 20, 36, 32, None, None, None, None, None, None, 43, 23, 9, 22, None, None, 35, 39, 18, 41, None, 44, None, 40, None, None, None, None, 29, 21, 37, 26, None, 46, None, 45, 42, 33, 24, None, None, None, 50, None, None, None, 48, None, 27, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, 49, 30, None, None, None, 34, 38], 33, 24, False), ([1, 18, 2, None, None, 3, 4, 17, 10, 5, 6, None, None, 21, 12, 8, 11, 31, 7, None, None, 37, 15, None, 9, 29, 14, None, 35, 24, 20, 44, None, 19, 25, 22, 13, 41, 32, 39, 16, 36, None, None, None, 27, None, None, None, 23, None, None, 47, 30, 26, None, 49, None, 50, None, None, None, None, 45, 28, None, None, None, None, 43, None, None, None, 34, 38, None, 33, None, None, None, None, None, 46, 40, None, None, None, None, 48, None, 42], 28, 30, True), ] for is_cousins in [is_cousins_one_list, is_cousins_double_list, ]: for test_tree_list, test_x, test_y, expected_output in test_cases: assert is_cousins(ConstructTree.build_tree_leetcode(test_tree_list).root, test_x, test_y) is expected_output, \ is_cousins.__name__
None, 7, 6, None, None, 1, 3, None, 4, 9, 8, None, None, 0, None, 3, 4, None, 8, None, 1, None, None, 2, 2, None, None, 4, None, None, None, 3, None, None, 2, None, None, None, 4, None, 5, None, None, None, None, 2, None, 5, None, None, None, None, None, None, 2, 7, 5, None, 6, None, None, None, None, 2, None, 0, None, 3, None, 1, None, 9, 4, None, 3, None, None, None, None, None, None, None, 5, 5, 7, None, None, 1, None, 4, 6, None, None, None, 2, None, 5, 9, 0, 6, 2, None, None, None, None, None, None, None, None, None, None, None, None, 5, None, 7, None, 2, 9, None, None, 1, None, None, None, 1, 6, None, 6, None, None, 0, 8, None, 4, None, None, None, None, 4, None, None, 0, None, 6, 0, None, None, None, 4, None, None, None, None, None, 0, None, None, None, None, None, None, None, None, None, None, None, None, 0, 5, 4, 2, 6, 4, 5, 3, 4, None, None, 5, None, None, None, None, 4, None, None, 3, 6, 2, 0, None, 6, 6, None, None, None, None, 0, 6, None, None, None, 3, 9, 4, None, None, None, None, None, 0, None, None, 6, 7, 0, None, 9, 2, None, 3, 3, None, None, 8, None, 3, None, None, None, 8, 5, 3, None, 2, 4, None, 9, 6, 9, None, None, None, None, 6, None, 6, None, 5, 3, None, None, None, None, 4, None, None, None, 9, 0, 9, 7, 1, 1, None, 1, None, 1, 6, None, 5, None, 6, None, None, 1, None, None, None, None, None, None, 5, None, None, None, None, None, 3, None, 6, 1, None, 0, 2, None, None, 0, None, None, 0, None, None, None, None, None, 3, None, None, 8, None, None, 5, 3, 3, None, None, None, None, None, None, None, 3, None, None, 0, 8, 7, None, None, 8, 1, None, None, None, None, None, None, 7, None, None, None, None, None, None, None, None, None, None, None, 5, 2, None, 2, 6, None, None, None, None, None, None, None, 1, 5, 0, None, None, 2, None, 7, None, None, 6, None, None, None, None, None, None, None, None, None, None, None, None, None, 8, None, None, None, None, 3, None, None, 4, None, None, 2, None, None, None, None, 0, 3, None, None, None, None, None, 7, None, 8, None, None, None, None, 8, 5, None, 3, 4, None, None, None, 8, None, None, None, None, None, None, None, None, None, 3, 7, None, None, None, 4, 0, 3, None, None, 6, None, None, None, None, None, None, None, None, None, None, None, None, 8, None, None, None, None, None, 2, None, None, None, None, None, None, None, None, None, 0, None, None, None, 2, None, None, None, 8, 2, None, None, None, None, None, None, None, 8, None, None, None, None, None, None, None, None, None, None, 2, None, None, None, 2, 5, None, None, None, None, None, None, None, None, None, None, None, 2, None, None, None, None, None, 8, None, None, None, None, None, None, None, None, None, None, 0, 5, None, 1, 1]), ] for test_tree_list, test_value, test_depth, expected_output in test_cases: get_tree = BinaryTree(add_one_row((ConstructTree.build_tree_leetcode(test_tree_list).root if test_tree_list else None), test_value, test_depth)).leetcode_traversal() assert CompareTree.compare_leetcode_traversal(get_tree, expected_output)
predecessor.right = self.current_node self.current_node = self.current_node.left # Revert the changes made in if part to restore the original tree # i.e., fix the right child of predecessor else: predecessor.right = None return_val = self.current_node.val self.current_node = self.current_node.right return return_val return None for bst_iterator_type in [ BSTIteratorMorrisTraversal, BSTIteratorStack, ]: test_bst_iterator = bst_iterator_type( ConstructTree.build_tree_leetcode([7, 3, 15, None, None, 9, 20]).root) assert test_bst_iterator.has_next() assert 3 == test_bst_iterator.next() assert 7 == test_bst_iterator.next() assert test_bst_iterator.has_next() assert 9 == test_bst_iterator.next() assert test_bst_iterator.has_next() assert 15 == test_bst_iterator.next() assert test_bst_iterator.has_next() assert 20 == test_bst_iterator.next() assert not test_bst_iterator.has_next() assert test_bst_iterator.next() is None assert test_bst_iterator.next() is None
:return: whether the tree mirror around the center line """ tree_node_queue = [root.left, root.right] while tree_node_queue: left_tree_node = tree_node_queue.pop(0) right_tree_node = tree_node_queue.pop(0) if left_tree_node is None and right_tree_node is None: continue if left_tree_node is None or right_tree_node is None or left_tree_node.val != right_tree_node.val: return False tree_node_queue.extend([ left_tree_node.left, right_tree_node.right, left_tree_node.right, right_tree_node.left ]) return True test_cases = [ ([1, 2, 2, 3, 4, 4, 3], True), ([1, 2, 2, None, 3, None, 3], False), ([1, 2, 2, 3, None, None, 3], True), ([1], True), ([1, 2], False), ([1, None, 2], False), ] for test_tree_list, expected_output in test_cases: assert is_symmetric( ConstructTree.build_tree_leetcode( test_tree_list).root) is expected_output
# start at second level left_to_right = False while curr_level: levels.append([x.val for x in curr_level]) curr_level.reverse() for node in curr_level: if left_to_right: if node.left: next_level.append(node.left) if node.right: next_level.append(node.right) else: if node.right: next_level.append(node.right) if node.left: next_level.append(node.left) curr_level = next_level left_to_right = not left_to_right next_level = [] return levels test_cases = [ ([3, 9, 20, None, None, 15, 7], [[3], [20, 9], [15, 7]]), ] for test_tree, expected_output in test_cases: assert zigzag_level_order(root=ConstructTree.build_tree_leetcode( test_tree).root) == expected_output
return self.root test_cases = [ ([1], (2, 1), [1, 2]), ([1, 2, 3, 4, 5, 6], (7, 3), (8, 4), [1, 2, 3, 4, 5, 6, 7, 8]), ([ 43, 7, 46, 23, 31, 5, 50, 27, 2, 8, 26, 13, 43, 18, 37, 8, 26, 18, 14, 42, 40, 23, 49, 42, 8, 10, 6, 26, 9, 37, 9, 9, 28, 44, 3, 17, 10, 38, 2, 47 ], (15, 42), (2, 40), (6, 40), (9, 23), (49, 23), (23, 49), (7, 49), (4, 42), (4, 42), (14, 8), (28, 8), (2, 10), (35, 10), (28, 6), (48, 6), (47, 26), (23, 26), (1, 9), (15, 9), (20, 37), (14, 37), (37, 9), (4, 9), (5, 9), (7, 9), (41, 28), (26, 28), (11, 44), (29, 44), (38, 3), (29, 3), (9, 17), (33, 17), (13, 10), (44, 10), (23, 38), (16, 38), (48, 2), (0, 2), (34, 47), [ 43, 7, 46, 23, 31, 5, 50, 27, 2, 8, 26, 13, 43, 18, 37, 8, 26, 18, 14, 42, 40, 23, 49, 42, 8, 10, 6, 26, 9, 37, 9, 9, 28, 44, 3, 17, 10, 38, 2, 47, 15, 2, 6, 9, 49, 23, 7, 4, 4, 14, 28, 2, 35, 28, 48, 47, 23, 1, 15, 20, 14, 37, 4, 5, 7, 41, 26, 11, 29, 38, 29, 9, 33, 13, 44, 23, 16, 48, 0, 34 ]), ] for test_input in test_cases: test_object = CBTInserter( ConstructTree.build_tree_leetcode(test_input[0]).root) for test_add_val, test_parent_val in test_input[1:-1]: assert test_object.insert(test_add_val) == test_parent_val get_tree = BinaryTree(test_object.get_root()).leetcode_traversal() assert CompareTree.compare_leetcode_traversal(get_tree, test_input[-1])
mark_parent(current_node.right, current_node) assert root is not None and target is not None mark_parent(root) exploration_queue = deque([(target, 0)]) seen = {target} while exploration_queue: if exploration_queue[0][1] == k: return [node.val for node, _ in exploration_queue] current_node, depth = exploration_queue.popleft() for neighbor in (current_node.left, current_node.right, current_node.parent): if neighbor and neighbor not in seen: seen.add(neighbor) exploration_queue.append((neighbor, depth + 1)) return [] test_tree = ConstructTree.build_tree_leetcode([0, 1, None, 3, 2]) target_node = test_tree.root.left.right assert [1] == distance_k_list_1(test_tree.root, target_node, 1) assert [1] == distance_k_list_2(test_tree.root, target_node, 1) test_tree = ConstructTree.build_tree_leetcode( [3, 5, 1, 6, 2, 0, 8, None, None, 7, 4]) target_node = test_tree.root.left assert [7, 4, 1] == distance_k_list_1(test_tree.root, target_node, 2) assert [7, 4, 1] == distance_k_list_2(test_tree.root, target_node, 2)
None, None, None, None, None, 0, None, None, None, None, None, None, None, 0, 0, 0, 0, 0, None, 0, 0, None, None, None, 0, 0, 0, 0, 0, None, None, None, None, None, 0, 0, 0, None, 0, None, None, None, None, 0, 0, 0, None, None, None, 0, None, None, 0, 0, None, None, 0, 0, None, None, None, None, None, None, None, 0, None, 0, None, None, None, None, 0, 0, None, None, 0, 0, 0, 0, 0, None, None, 0, None, None, 0, 0, 0, 0, 0, 0, None, None, None, 0, None, None, None, 0, 0, None, None, None, None, None, None, None, None, None, None, 0, 0, None, 0, None, None, 0, None, 0, None, None, None, None, None, None, None, 0, None, None, None, None, None, None, None, 0, None, 0, None, None, 0, None, None, 0, 0, None, None, None, 0, None, None, 0, 0, None, None, 0, 0, 0, 0, 0, 0, 0, 0, None, None, 0, 0, None, None, None, None, None, None, None, None, 0, 0, 0, None, None, None, None, 0, None, 0, None, 0, None, None, None, None, 0, 0, None, None, None, None, None, 0, 0, 0, None, 0, None, None, None, 0, None, None, 0, 0, None, None, None, None, 0, None, None, None, None, None, 0, 0, None, None, None, None, None, None, 0, 0, None, None, None, 0, None, None, None, None, None, None, None, None, None, None, 0, None, None, None, None, None, 0, 0, None, None, None, None, None, None, 0, 0, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, 0 ], 360), ] for min_camera_cover in [ min_camera_cover_dynamic_programming, min_camera_cover_greedy, ]: for test_tree_list, expected_count in test_cases: assert min_camera_cover( ConstructTree.build_tree_leetcode( test_tree_list).root) == expected_count
19, 63, 41, 91, 43, 49, 46, 87, 74, 49, 1, 21, 62, 6, 34, 77, -9, 61, 7, 45, 63, 7, 16 ], [ 86, 63, 61, 72, 13, 24, 91, 59, 48, 14, 77, 92, 84, 76, 82, 63, 84, 84, 94, 47, 40, 75, 20, -9, 24, 74, 51, 91, 83, 17, 42, 49, 88, 57, 85, 1, 94, 28, 36, 78, 53, 27, 25, 46, 97, 58, 12, 33, 6, 87, 9, 83, 90, 11, 61, 89, 46, 86, 81, 53, 59 ], [ 29, 47, 97, 0, 9, 17, 91, 45, 9, 61, 21, 64, 69, 44, 12, 2, -8, 88, -8, 93, 86, 97, 72, 50, 47, 70, 62, -3, -5, 59, 15, -3, 37, 20, -2, 8, 90, 61, 15, 12, 95, 73, 11, 76, 76, 49, 51 ], [ 42, -9, 80, 70, 31, 78, 98, 7, 57, -3, -7, 31, 42, 62, 17, 7, 63, 83, 51, 76, 77, 40, 95, 27, 55, 61 ], [95, 93, 19, 37, 73, 75, 22, -7, 99, 94, 63, 39, 77, -2, 15], [69, 33, 9, 42, 69, 35, 36, 11, 52], [51, 50, 30], [63], [56], [28]]), ] for test_tree_list, expected_output in test_cases: if test_tree_list: assert ConstructTree.build_tree_leetcode( test_tree_list).layer_traversal_by_layer() == expected_output else: assert BinaryTree( root=None).layer_traversal_by_layer() == expected_output for test_tree_list, expected_output in test_cases: assert level_order((ConstructTree.build_tree_leetcode(test_tree_list).root if test_tree_list else None)) == \ expected_output
""" Given the root of a binary search tree, and an integer k, return the kth (1-indexed) smallest element in the tree. """ from _BST import BST from _Binary_Tree import ConstructTree, TreeNode, TREE_NODE_TYPE def kth_smallest_element(root: TreeNode, k: int) -> TREE_NODE_TYPE: """ :param root: root node of a non empty binary search tree, 1 <= # of nodes <= 10000 :param k: 1 <= k <= # of nodes :return: value of the kth smallest element in the binary search tree """ return BST(root).kth_smallest_element(k) test_cases = [([3, 1, 4, None, 2], 1, 1), ([5, 3, 6, 2, 4, None, None, 1], 3, 3), ([41, 37, 44, 24, 39, 42, 48, 1, 35, 38, 40, None, 43, 46, 49, 0, 2, 30, 36, None, None, None, None, None, None, 45, 47, None, None, None, None, None, 4, 29, 32, None, None, None, None, None, None, 3, 9, 26, None, 31, 34, None, None, 7, 11, 25, 27, None, None, 33, None, 6, 8, 10, 16, None, None, None, 28, None, None, 5, None, None, None, None, None, 15, 19, None, None, None, None, 12, None, 18, 20, None, 13, 17, None, None, 22, None, 14, None, None, 21, 23], 25, 24), ([5, 3, 6, 2, 4, None, None, 1], 6, 6), ] for test_tree_list, test_k, expected_output in test_cases: assert kth_smallest_element(ConstructTree.build_tree_leetcode(test_tree_list).root, test_k) == expected_output
272, 258, None, 266, 182, -43, 66, 48, None, None, -122, None, 205, 68, None, 275, None, -165, None, None, None, None, -6, 160, None, None, None, 80, None, 29, 278, None, None, None, None, None, 185, None, None, None, None, None, None, None, -70, -89, None, None, None, None, None, 60, None, None, None, None, None, 99, 101, 16, None, -44, None, -108, -67, None, None, -15, None, None, None, None, None, 53, None, None, None, 75, -190, 86, 4, 73, 19, None, 63, 50, 245, 220, None, -184, None, None, 113, None, None, None, 206, 42, None, None, None, -26, -87, None, None, -86, None, -105, 222, 199, -145, 268, None, None, None, None, None, None, None, 162, 184, None, 155, None, None, None, None, None, None, None, 114, None, None, None, None, None, None, None, None, 92, None, None, 9, 271, None, None, None, 218, None, None, None, None, -25, -47, 190, None, None, None, -148, None, None, None, None, None, None, None, None, 27, None, None, None, 179, -166, None, None, -75, None, None, 242, None, None, None, None, None, None, -144, 230, 111, None, -27, None, 225, 149, None, 262, None, None, 117, 200, 234, None, None, -124, None, None, None, None, None, 213, None, None, None, 281, None, 188, -72, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, 58, 297, None, None, None, None, None, None, None, None, None, None, None, None, 285, None, None, 207, None, -39, 292, -171, -52, None, None, None, None, None, None, -71, -114, -176 ]), ] for test_pre_order, test_in_order, expected_tree_list in test_cases: assert CompareTree.compare_leetcode_traversal( ConstructTree.build_tree_pre_in(test_pre_order, test_in_order).leetcode_traversal(), expected_tree_list)
# end of binary tree return False # Current level match up and next node match up with either child node return probe_head.val == probe_root.val and \ (downward_probe(probe_head.next, probe_root.left) or downward_probe(probe_head.next, probe_root.right)) if not root: # Empty tree return False # Start downward path from probe_root # or wait until child node to start downward path return downward_probe(head, root) or is_sub_path( head, root.left) or is_sub_path(head, root.right) test_cases = [ ([4, 2, 8], [1, 4, 4, None, 2, 2, None, 1, None, 6, 8, None, None, None, None, 1, 3], True), ([1, 4, 2, 6], [1, 4, 4, None, 2, 2, None, 1, None, 6, 8, None, None, None, None, 1, 3], True), ([1, 4, 2, 6, 8], [1, 4, 4, None, 2, 2, None, 1, None, 6, 8, None, None, None, None, 1, 3], False) ] for build_list, build_tree, expected_output in test_cases: assert is_sub_path( LinkedList.create_linked_list(build_list).head, ConstructTree.build_tree_leetcode(build_tree).root) is expected_output
None, None, None, None, 6, 8, 6, 9, 10, None, 8, None, None, None, None, None, None, None, None, None, 7, None, 3, 6, 1, 6, 1, 7, None, 10, None, None, None, None, None, None, None, 10, None, None, None, None, 5, 5, 6, 7, 7, 6, 9, 2, None, 9, 7, 3, None, 7, 7, None, None, 8, 2, 7, 5, 4, 4, None, None, 10, 9, None, 9, 2, None, 9, None, 2, None, None, None, None, None, None, None, 2, None, None, None, 1, 4, None, 2, None, 4, None, None, 2, None, 1, None, 1, None, None, 4, None, 8, None, 10, None, None, None, 10, 7, None, None, None, None, None, None, None, None, None, None, None, None, 9, 4, 8, 6, None, None, None, None, None, 6, None, 2, 2, None, None, None, None, None, None, None, None, None, None, None, 6, 10, None, 7, 6, 5, 1, 3, 3, 1, 6, 6, 5, None, None, 5, None, None, None, 2, None, None, None, None, 5, 2, 1, 4, 9, 6, 10, 5, 6, 3, 6, 3, 4, 7, 1, None, 4, None, None, None, None, 9, None, None, None, None, None, 1, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, 8, None, None, 2, 3, None, 2, None, None, 5, 10, None, None, 6, None, 10, None, None, 10, 3, None, None, None, None, None, None, None, None, None, None, None, None, None, None, 9, None, 4, None, 4, 4, 6, 5, 4, 2, 6, None, 3, 9, None, None, None, None, 10, None, 4, None, 6, None, None, 1, None, None, None, 2, None, None, 1, 2, 10, 8, 10, None, 10, 5, 8, None, 8, 5, None, 3, None, None, None, None, 8, None, None, None, None, 6, None, None, None, 7, None, None, None, 7, None, None, None, 3, None, None, None, 7, 7, None, None, None, 2, None, None, 9, None, 2, 8, 7, 10, 5, None, 9, None, None, None, None, None, None, 6, 7, None, None, 4, None, None, None, None, None, None, None, None, None, None, None, 2, None, None, 7, None, 8, 9, 5, None, 6, 5, None, 5, None, None, None, None, None, None, None, None, None, None, None, None, 10, 4, None, None, None, None, None, 1, None, 2, None, None, 8, 3, 6, 4, None, None, 6, 8, None, None, None, None, None, None, 1, 8, None, None, None, None, None, 1, None, None, None, None, None, 4, None, None, 6, None, 9, None, 1, 8, None, 3, 2, None, None, 2, 7, 3, 1, 1, None, None, 6, None, 6, None, None, None, None, None, 3, None, 8, None, None, None, 9, 9, None, 10, 1, None, 6, 4, None, None, None, None, 5, 5, None, 9, None, None, None, 8, 6, None, None, None, None, None, None, None, None, None, None, None, None, None, 2, None, None, None, 5, 8, None, None, None, None, 3, None, None, None, None, 10, 1, 4, None, 4, None, 6, 1, None, None, None, 2, 8, 1, None, 10, None, None, None, None, 6, None, None, None, 3], 13), ] for longest_zig_zag in [longest_zig_zag_all, ]: for test_tree_list, expected_output in test_cases: assert longest_zig_zag(ConstructTree.build_tree_leetcode(test_tree_list).root) == expected_output, \ longest_zig_zag.__name__