Given two Binary Trees, write a function that returns true if two trees are mirror of each other, else false. For example, the function should return true for following input trees.
This problem is different from the problem discussed here.
For two trees ‘a’ and ‘b’ to be mirror images, the following three conditions must be true:
- Their root node’s key must be same
- Left subtree of root of ‘a’ and right subtree root of ‘b’ are mirror.
- Right subtree of ‘a’ and left subtree of ‘b’ are mirror.
Below is implementation of above idea.
Time Complexity : O(n)
This article is contributed by Ashish Gupta. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
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.
- Check if two trees are Mirror | Set 2
- Iterative method to check if two trees are mirror of each other
- Check if two trees are mirror of each other using level order traversal
- Check mirror in n-ary tree
- Check if two trees have same structure
- Check if all levels of two trees are anagrams or not
- Check if leaf traversal of two Binary Trees is same?
- Iterative function to check if two trees are identical
- Iterative Approach to check if two Binary Trees are Isomorphic or not
- Generic Trees(N-array Trees)
- Mirror of n-ary Tree
- Total number of possible Binary Search Trees and Binary Trees with n keys
- Symmetric Tree (Mirror Image of itself)
- Find mirror of a given node in Binary tree
- Append the elements of queue in mirror-inverse order
- Sum of the mirror image nodes of a complete binary tree in an inorder way
- Number of edges in mirror image of Complete binary tree
- Create a mirror tree from the given binary tree
- Convert a Binary Tree into its Mirror Tree
- B*-Trees implementation in C++