Check if 2 nodes are mirror of each other

In this tutorial we shall see how to check if 2 trees are mirror to each other.

Problem Statement:

You are given root node of 2 trees. You need to check if those 2 trees are mirror to each other.
For example:
Below 2 trees are mirror to each other.
check_2_binary_trees_are_mirror_of_each_other
As you can see above, except root node, the left child will become right and vice versa. This is the meaning of mirror of binary tree.
From the above image we can arrive at below points that return true.
1. If the current is root node, then continue.
2. If the left child of the tree 1 is equal to the right child value return true.
3. If the right child value is equal to the left child value, return true.
4. If both are NULL, then return true.
We can also define below false statements:
1. If right child exists in tree 1 and no child at tree 2 return false, same holds for vice versa.
2. If the right child value is different than the left child value of tree 2, return false.
Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *