Check if two binary trees are a mirror image of each other

Posted by N.K. Chauhan on Mar 31, 2024

Two binary trees T1 an T2 and given, the task is to identify if "T1" is a mirror image of "T2".


Solutions

Method 1: Recursion

If two binary trees are mirror image of each other, following conditions must be true:

1) The value of their "root" node should be same.
2) Left subtree of root of "T1" should be a mirror image of right subtree of root of "T2".
3) Right subtree of root of "T1" should be a mirror image of left subtree of root of "T2".

Below is the implementation for above mentioned idea.

Complexity
The time complexity of this solution is O(n) and space complexity is O(h), where "h" is height of binary tree.

Related


Print the left view of a binary tree

In-order Tree Traversal - Iterative and Recursive

Print Nodes in Top View of a Binary Tree

Create a mirror tree from a given binary tree

Print/Traverse a Binary Tree in Zigzag Order

Print diagonal traversal of a binary tree

Level Order or Breadth First traversal of Binary Tree

Print a Binary Tree in Vertical Order

Pre-order Tree Traversal - Iterative and Recursive

Write a program to print height/depth of a Binary Tree

Find the diameter or width of a binary tree