horizontal_distance - 1) finding_x_for_each_node__pre_order(root.right, x_table, horizontal_distance + 1) def vertical_order(root): x_table: Dict[int, list] = dict() # map of number, list finding_x_for_each_node__pre_order(root, x_table) for x in sorted(x_table.keys()): print_array_inline(x_table[x]) if __name__ == "__main__": tree_root = BinaryTree.single_line_input(input_array()) BinaryTree.display(tree_root) vertical_order(tree_root) """ 1 / \ 2 3 / \ \ 4 5 6 / \ 7 8 1 2 3 4 5 -1 6 7 -1 -1 8 -1 -1 -1 -1 -1 -1 Output 7 4 2
left_child_diameter = diameter_of_binary_tree(root.left) right_child_diameter = diameter_of_binary_tree(root.right) # current node's diameter left_depth = find_depth(root.left) right_depth = find_depth(root.right) curr_node_diameter = left_depth + right_depth return max(curr_node_diameter, left_child_diameter, right_child_diameter) if __name__ == "__main__": tree_input = input_array(prompt="") root = BinaryTree.single_line_input(tree_input) BinaryTree.display(root) print("diameter : ", diameter_of_binary_tree(root)) """ 1 / \ 2 3 / \ 4 5 1 2 3 4 5 -1 -1 -1 -1 -1 -1 """ """ 0 / \