Find Mirror of a Binary Tree - Algorithm, Visualization, Examples

Visualization Player

Problem Statement

Given a binary tree, your task is to convert it into its mirror image. In a mirrored binary tree, the left and right children of all nodes are swapped recursively. You need to find this mirrored structure and optionally visualize or represent it in your preferred format.

Examples

Input Tree Level Order Output Description
[1, 2, 3, 4, 5, null, 6]
[[1], [3, 2], [6, 5, 4]] Standard case: Mirror of a tree with three levels and uneven children
[1]
[[1]] Edge case: Single-node tree remains the same when mirrored
[] [] Edge case: Empty tree has no mirror
[1, 2, null, 3, null, null, null, 4]
[[1], [null, 2], [null, 3], [null, 4]] Left-skewed tree mirrored into a right-skewed structure
[1, null, 2, null, null, null, 3]
[[1], [2, null], [3, null]] Right-skewed tree mirrored into a left-skewed structure
[7, 3, 9, 1, 5, 8, 10]
[[7], [9, 3], [10, 8, 5, 1]] Full binary tree with mirrored structure at each level

Solution

Understanding the Problem

We are given a binary tree and asked to return its mirror image. Mirroring a binary tree means swapping the left and right subtrees of every node in the tree recursively. The root stays the same, but its children, and all children below, are flipped horizontally. This transformation turns a left-heavy tree into a right-heavy one and vice versa.

The challenge lies in performing this transformation correctly for all types of binary trees — including balanced trees, skewed trees, and edge cases like a single-node or empty tree.

Step-by-Step Solution with Example

Step 1: Define the Task Clearly

We want to traverse the binary tree and, for every node we visit, swap its left and right child. This needs to be done for all nodes in the tree, which means recursion is a natural fit.

Step 2: Pick an Example

Let’s take a simple binary tree:


        1
       /       2   3

Its mirror image would look like this:


        1
       /       3   2

Step 3: Recursively Swap Children

Starting at the root node (1), we swap the left (2) and right (3) children. Then, we go deeper and apply the same swapping logic recursively on node 2 and node 3.

If node 2 and 3 have children, their left and right children would also be swapped, and so on.

Step 4: Write the Recursive Logic

The recursive steps can be described as:

  1. If the current node is null, return null (base case)
  2. Swap its left and right child
  3. Recursively call the function on the left child
  4. Recursively call the function on the right child

Edge Cases

Case 1: Left-Skewed Tree

Example: [1, 2, null, 3] becomes [1, null, 2, null, 3]

This demonstrates how the left-only structure becomes right-only after mirroring.

Case 2: Right-Skewed Tree

Example: [1, null, 2, null, 3] becomes [1, 2, null, 3]

Right-only nodes become left-only, following the same recursive logic.

Case 3: Single Node Tree

Input: [1]
Output remains: [1]

Since there's no child to swap, the tree stays the same.

Case 4: Empty Tree

Input: [] → Output: []

No nodes to process. Function should handle this safely and return null or equivalent.

Finally

This problem is a classic example of recursive tree traversal. By understanding the structure of the binary tree and applying a simple left-right swap at every node, we can generate its mirror efficiently.

It’s important to consider edge cases like null nodes, skewed trees, and minimal structures during implementation. Always test your function with various types of trees to ensure robustness.

Algorithm Steps

  1. Start at the root node of the binary tree.
  2. If the current node is null, return.
  3. Swap the left and right children of the current node.
  4. Recursively call the mirror function on the left subtree.
  5. Recursively call the mirror function on the right subtree.
  6. The binary tree is now mirrored.

Code

C
C++
Python
Java
JS
Go
Rust
Kotlin
Swift
TS
#include <stdio.h>
#include <stdlib.h>

typedef struct TreeNode {
    int val;
    struct TreeNode* left;
    struct TreeNode* right;
} TreeNode;

TreeNode* createNode(int val) {
    TreeNode* node = (TreeNode*)malloc(sizeof(TreeNode));
    node->val = val;
    node->left = node->right = NULL;
    return node;
}

TreeNode* mirror(TreeNode* root) {
    if (root == NULL) return NULL;
    TreeNode* temp = root->left;
    root->left = root->right;
    root->right = temp;
    mirror(root->left);
    mirror(root->right);
    return root;
}

int main() {
    TreeNode* root = createNode(1);
    root->left = createNode(2);
    root->right = createNode(3);
    root->left->left = createNode(4);
    root->right->left = createNode(5);
    root->right->right = createNode(6);
    mirror(root);
    printf("Tree mirrored successfully\n");
    return 0;
}

Comments

💬 Please keep your comment relevant and respectful. Avoid spamming, offensive language, or posting promotional/backlink content.
All comments are subject to moderation before being published.


Loading comments...