def test_build_tree(node_list: List[Optional[int]]) -> BinaryTree: if not node_list or node_list[0] is None: return BinaryTree(None) return_tree = BinaryTree(TreeNode(node_list[0])) node_queue = deque([return_tree.root]) node_child_counter = 0 current_node = None for i in range(1, len(node_list)): if node_child_counter == 0: current_node = node_queue.popleft() if node_list[i] is not None: if node_child_counter: current_node.right = TreeNode(node_list[i]) node_queue.append(current_node.right) else: current_node.left = TreeNode(node_list[i]) node_queue.append(current_node.left) node_child_counter = (node_child_counter + 1) % 2 return return_tree
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])
None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, None, 1, 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)
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)
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
return None right = invert_tree(root.right) if root.right else None left = invert_tree(root.left) if root.left else None root.left, root.right = right, left return root node = TreeNode(4) node.left = TreeNode(2) node.left.left = TreeNode(1) node.left.right = TreeNode(3) node.right = TreeNode(7) node.right.left = TreeNode(6) node.right.right = TreeNode(9) tree = BinaryTree(node) assert tree.preorder_traversal() == [4, 2, 1, 3, 7, 6, 9] assert tree.inorder_traversal() == [1, 2, 3, 4, 6, 7, 9] assert tree.postorder_traversal() == [1, 3, 2, 6, 9, 7, 4] assert tree.layer_traversal_by_layer() == [[4], [2, 7], [1, 3, 6, 9]] assert tree.leetcode_traversal() == [ 4, 2, 7, 1, 3, 6, 9, None, None, None, None, None, None, None, None ] new_tree_root = invert_tree(node) new_tree = BinaryTree(new_tree_root) assert new_tree.preorder_traversal() == [4, 7, 9, 6, 2, 3, 1] assert new_tree.inorder_traversal() == [9, 7, 6, 4, 3, 2, 1] assert new_tree.postorder_traversal() == [9, 6, 7, 3, 1, 2, 4] assert new_tree.layer_traversal_by_layer() == [[4], [7, 2], [9, 6, 3, 1]] assert new_tree.leetcode_traversal() == [
None, 363, None, 440, None, None, 325, 247, None, None, None, None, 289, None, None, 432, None, None, None, None, None, None, None, 484, None, None, 321, 269, None, None, None, None, None, 373, 485, 403, 344, 243, 254, 202, None, 473, 347, 455, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, 308, None, None, None, None, None, None, None, None, None, 142, None, None, 369, None, None, None, None, 436, None, 376, 420, None, None, None, None, None, None, 115, 263, None, None, None, 399, None, None, 371, 234, 351, None, 474, None, 441, None, None, None, 483, 418, None, None, 358, 427, None, None, None, None, 349, None, 365, None, 389, None, None, None, None, None, 384, 438, None, 410, None, None, 378, 350, None, 479, None, None, None, 464, None, 332, 405, None, None, None, None, None, None, None, None, 447, 136, 164, 280, None, 401, None, 456, None, None, 422, None, None, None, None, None, None, None, None, 486, None, None, None, None, 493, None, None, None, None, None, None, 458, None, None, None, None, None, None, None, None, 490, None, None, None, 494, None, None, None, None, None, None, 150, 157, 206, 396, None, 337, None, None, None, 495, None, 481, None, None, None, None, None, None, None, None, None, None, 159, 210, 307, 377, 259, None, None, 421, None, None, None, None, None, None, 268, None, 431, 223, None, 342, None, 491, None, None, None, None, None, None, None, 460, 275, 224, None, None, None, None, None, None, None, None, 322, 239, 463 ], [463]), ] for test_tree_list, expected_list in test_cases: got_tree = BinaryTree( sub_tree_with_all_deepest_nodes( ConstructTree.build_tree_leetcode(test_tree_list).root)) assert CompareTree.compare_leetcode_traversal( got_tree.leetcode_traversal(), expected_list)