Given two binary trees, the task is to check whether the two binary trees is a mirror of each other or not.
Mirror of a Binary Tree: Mirror of a Binary Tree T is another Binary Tree M(T) with left and right children of all non-leaf nodes interchanged.
Trees in the above figure are mirrors of each other.
A recursive solution and an iterative method using inorder traversal to check whether the two binary trees is a mirror of each other or not have been already discussed. In this post a solution using level order traversal has been discussed.
The idea is to use a queue in which two nodes of both the trees which needs to be checked for equality are present together. At each step of level order traversal, get two nodes from the queue, check for their equality and then insert next two children nodes of these nodes which need to be checked for equality. During insertion step, first left child of first tree node and right child of second tree node are inserted. After this right child of first tree node and left child of second tree node are inserted. If at any stage one node is NULL and other is not, then both trees are not a mirror of each other.
Below is the implementation of above approach:
Time complexity: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Iterative method to check if two trees are mirror of each other
- Level order traversal of Binary Tree using Morris Traversal
- Construct Full Binary Tree using its Preorder traversal and Preorder traversal of its mirror tree
- Print a Binary Tree in Vertical Order | Set 3 (Using Level Order Traversal)
- Connect Nodes at same Level (Level Order Traversal)
- Flatten Binary Tree in order of Level Order Traversal
- Insertion in n-ary tree in given order and Level order traversal
- General Tree (Each node can have arbitrary number of children) Level Order Traversal
- Print nodes of a Binary Search Tree in Top Level Order and Reversed Bottom Level Order alternately
- Level order traversal line by line | Set 2 (Using Two Queues)
- Check if two trees are Mirror
- Check if two trees are Mirror | Set 2
- Level order traversal with direction change after every two levels
- Level order traversal with direction change after every two levels | Recursive Approach
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Check if the given array can represent Level Order Traversal of Binary Search Tree
- Check if the level order traversal of a Binary Tree results in a palindrome
- Level order traversal line by line | Set 3 (Using One Queue)
- Level order traversal in spiral form | Using one stack and one queue
- Zig Zag Level order traversal of a tree using single array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.