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
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- 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
- Iterative function to check if two trees are identical
- Check if leaf traversal of two Binary Trees is same?
- 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
- AA Trees | Set 1 (Introduction)