Case 1: Normal Binary Tree with Both Subtrees
In a typical binary tree where each node has both left and right children, mirroring is performed by swapping each node’s left and right child recursively. Starting at the root, you first swap the root's children, then apply the same operation on the new left and right subtrees. For example, a tree like [1, 2, 3] becomes [1, 3, 2]. This approach ensures the entire structure is flipped across a central vertical axis.
Case 2: Left-Skewed Tree
In a left-skewed tree (a tree where each node only has a left child), the mirror image will become a right-skewed tree. For example, the input [1, 2, null, 3] becomes [1, null, 2, null, 3]. Each node that originally had only a left child will now only have a right child. This transformation visually inverts the direction of the tree.
Case 3: Right-Skewed Tree
For a right-skewed tree (each node only has a right child), the mirror would be a left-skewed tree. For instance, [1, null, 2, null, 3] becomes [1, 2, null, 3]. The mirror process effectively flips the tree from right to left, following the same recursive swapping logic.
Case 4: Single-Node Tree
If the tree contains only one node, such as [1], the mirror of the tree will be the same as the original. There's no left or right child to swap, so the output remains unchanged. This is a natural base case for recursion when processing the mirror.
Case 5: Empty Tree
When the input tree is empty (represented as an empty array), the output is also an empty tree. There are no nodes to process, so this is the trivial base case where the function returns immediately or does nothing depending on implementation.