Given two n-ary trees, the task is to check if they are mirror of each other or not. Print “Yes” if they are mirror of each other else “No”.
Input : Node = 3, Edges = 2 Edge 1 of first N-ary: 1 2 Edge 2 of first N-ary: 1 3 Edge 1 of second N-ary: 1 2 Edge 2 of second N-ary: 1 3 Output : Yes Input : Node = 3, Edges = 2 Edge 1 of first N-ary: 1 2 Edge 2 of first N-ary: 1 3 Edge 1 of second N-ary: 1 2 Edge 2 of second N-ary: 1 3 Output : No
The idea is to use Queue and Stack to check if given N-ary tree are mirror of each other or not.
Let first n-ary tree be t1 and second n-ary tree is t2. For each node in t1, make stack and push its connected node in it. Now, for each node in t2, make queue and push its connected node in it.
Now, for each corresponding node do following:
While stack and Queue is not empty. a = top element of stack; b = front of stack; if (a != b) return false; pop element from stack and queue.
This article is contributed by Anuj Chauhan. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Convert a Binary Tree into its Mirror Tree
- Mirror of n-ary Tree
- Symmetric Tree (Mirror Image of itself)
- Find mirror of a given node in Binary tree
- Check if two trees are Mirror
- 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 if mirror image of a number is same if displayed in seven segment display
- Construct Full Binary Tree using its Preorder traversal and Preorder traversal of its mirror tree
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Check if a given Binary Tree is height balanced like a Red-Black Tree
- Check whether a binary tree is a complete tree or not | Set 2 (Recursive Solution)
- Check whether a binary tree is a full binary tree or not | Iterative Approach
- Check if a given graph is tree or not