Function to create a mirror of a binary search tree
(root: Optional[Node])
| 3 | |
| 4 | |
| 5 | def create_mirror_bst(root: Optional[Node]) -> Optional[Node]: |
| 6 | """Function to create a mirror of a binary search tree""" |
| 7 | |
| 8 | # If the tree is empty, return None |
| 9 | if root is None: |
| 10 | return None |
| 11 | |
| 12 | # Recursively create the mirror of the left and right subtrees |
| 13 | left_mirror: Optional[Node] = create_mirror_bst(root.left) |
| 14 | right_mirror: Optional[Node] = create_mirror_bst(root.right) |
| 15 | |
| 16 | # Swap left and right subtrees |
| 17 | root.left = right_mirror |
| 18 | root.right = left_mirror |
| 19 | return root |