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)
- Check if the given array can represent Level Order Traversal of Binary Search Tree
- Connect Nodes at same Level (Level Order Traversal)
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Print a Binary Tree in Vertical Order | Set 3 (Using Level Order Traversal)
- Construct BST from its given level order traversal
- Level Order Tree Traversal
- Reverse Level Order Traversal
- Level order traversal in spiral form
- Reverse Level Order traversal in spiral form
- Zig Zag Level order traversal of a tree using single queue
- Density of Binary Tree using Level Order Traversal
- Level order traversal with direction change after every two levels
- Level order traversal in spiral form | Using one stack and one queue
- Perfect Binary Tree Specific Level Order Traversal
- Perfect Binary Tree Specific Level Order Traversal | Set 2
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.