|
1 |
| -""" |
2 |
| -Problem Description: |
3 |
| -Given a binary tree, return its mirror. |
4 |
| -""" |
5 |
| - |
6 |
| - |
7 |
| -def binary_tree_mirror_dict(binary_tree_mirror_dictionary: dict, root: int): |
8 |
| - if not root or root not in binary_tree_mirror_dictionary: |
| 1 | +def mirror_subtree(tree: dict, root: int): |
| 2 | + if root not in tree: |
9 | 3 | return
|
10 |
| - left_child, right_child = binary_tree_mirror_dictionary[root][:2] |
11 |
| - binary_tree_mirror_dictionary[root] = [right_child, left_child] |
12 |
| - binary_tree_mirror_dict(binary_tree_mirror_dictionary, left_child) |
13 |
| - binary_tree_mirror_dict(binary_tree_mirror_dictionary, right_child) |
| 4 | + left, right = tree[root] |
| 5 | + tree[root] = [right, left] |
| 6 | + if left: |
| 7 | + mirror_subtree(tree, left) |
| 8 | + if right: |
| 9 | + mirror_subtree(tree, right) |
14 | 10 |
|
15 |
| - |
16 |
| -def binary_tree_mirror(binary_tree: dict, root: int = 1) -> dict: |
| 11 | +def mirror_binary_tree(tree: dict, root: int = 1) -> dict: |
17 | 12 | """
|
18 |
| - >>> binary_tree_mirror({ 1: [2,3], 2: [4,5], 3: [6,7], 7: [8,9]}, 1) |
| 13 | + Returns the mirror of the given binary tree starting from the root. |
| 14 | + |
| 15 | + >>> mirror_binary_tree({1: [2, 3], 2: [4, 5], 3: [6, 7], 7: [8, 9]}, 1) |
19 | 16 | {1: [3, 2], 2: [5, 4], 3: [7, 6], 7: [9, 8]}
|
20 |
| - >>> binary_tree_mirror({ 1: [2,3], 2: [4,5], 3: [6,7], 4: [10,11]}, 1) |
| 17 | + >>> mirror_binary_tree({1: [2, 3], 2: [4, 5], 3: [6, 7], 4: [10, 11]}, 1) |
21 | 18 | {1: [3, 2], 2: [5, 4], 3: [7, 6], 4: [11, 10]}
|
22 |
| - >>> binary_tree_mirror({ 1: [2,3], 2: [4,5], 3: [6,7], 4: [10,11]}, 5) |
| 19 | + >>> mirror_binary_tree({1: [2, 3], 2: [4, 5], 3: [6, 7], 4: [10, 11]}, 5) |
23 | 20 | Traceback (most recent call last):
|
24 | 21 | ...
|
25 |
| - ValueError: root 5 is not present in the binary_tree |
26 |
| - >>> binary_tree_mirror({}, 5) |
| 22 | + ValueError: Root 5 is not present in the binary tree |
| 23 | + >>> mirror_binary_tree({}, 5) |
27 | 24 | Traceback (most recent call last):
|
28 | 25 | ...
|
29 |
| - ValueError: binary tree cannot be empty |
| 26 | + ValueError: Binary tree cannot be empty |
30 | 27 | """
|
31 |
| - if not binary_tree: |
32 |
| - raise ValueError("binary tree cannot be empty") |
33 |
| - if root not in binary_tree: |
34 |
| - msg = f"root {root} is not present in the binary_tree" |
35 |
| - raise ValueError(msg) |
36 |
| - binary_tree_mirror_dictionary = dict(binary_tree) |
37 |
| - binary_tree_mirror_dict(binary_tree_mirror_dictionary, root) |
38 |
| - return binary_tree_mirror_dictionary |
| 28 | + if not tree: |
| 29 | + raise ValueError("Binary tree cannot be empty") |
| 30 | + if root not in tree: |
| 31 | + raise ValueError(f"Root {root} is not present in the binary tree") |
39 | 32 |
|
| 33 | + mirrored_tree = dict(tree) |
| 34 | + mirror_subtree(mirrored_tree, root) |
| 35 | + return mirrored_tree |
40 | 36 |
|
41 | 37 | if __name__ == "__main__":
|
| 38 | + import doctest |
| 39 | + doctest.testmod() |
| 40 | + |
42 | 41 | binary_tree = {1: [2, 3], 2: [4, 5], 3: [6, 7], 7: [8, 9]}
|
43 |
| - print(f"Binary tree: {binary_tree}") |
44 |
| - binary_tree_mirror_dictionary = binary_tree_mirror(binary_tree, 5) |
45 |
| - print(f"Binary tree mirror: {binary_tree_mirror_dictionary}") |
| 42 | + print(f"Original binary tree: {binary_tree}") |
| 43 | + mirrored_tree = mirror_binary_tree(binary_tree, 1) |
| 44 | + print(f"Mirrored binary tree: {mirrored_tree}") |
0 commit comments