# Check mirror in n-ary tree

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”.

Examples:

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.

`// C++ program to check if two n-ary trees are ` `// mirror. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// First vector stores all nodes and adjacent of every ` `// node in a stack. ` `// Second vector stores all nodes and adjacent of every ` `// node in a queue. ` `bool` `mirrorUtil(vector<stack<` `int` `> >& tree1, ` ` ` `vector<queue<` `int` `> >& tree2) ` `{ ` ` ` `// Traversing each node in tree. ` ` ` `for` `(` `int` `i = 1; i < tree1.size(); ++i) { ` ` ` `stack<` `int` `>& s = tree1[i]; ` ` ` `queue<` `int` `>& q = tree2[i]; ` ` ` ` ` `// While stack is not empty && Queue is not empty ` ` ` `while` `(!s.empty() && !q.empty()) { ` ` ` ` ` `// checking top element of stack and front ` ` ` `// of queue. ` ` ` `if` `(s.top() != q.front()) ` ` ` `return` `false` `; ` ` ` ` ` `s.pop(); ` ` ` `q.pop(); ` ` ` `} ` ` ` ` ` `// If queue or stack is not empty, return false. ` ` ` `if` `(!s.empty() || !q.empty()) ` ` ` `return` `false` `; ` ` ` `} ` ` ` ` ` `return` `true` `; ` `} ` ` ` `// Returns true if given two trees are mirrors. ` `// A tree is represented as two arrays to store ` `// all tree edges. ` `void` `areMirrors(` `int` `m, ` `int` `n, ` `int` `u1[], ` `int` `v1[], ` ` ` `int` `u2[], ` `int` `v2[]) ` `{ ` ` ` `vector<stack<` `int` `> > tree1(m + 1); ` ` ` `vector<queue<` `int` `> > tree2(m + 1); ` ` ` ` ` `// Pushing node in the stack of first tree. ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `tree1[u1[i]].push(v1[i]); ` ` ` ` ` `// Pushing node in the queue of second tree. ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `tree2[u2[i]].push(v2[i]); ` ` ` ` ` ` ` `mirrorUtil(tree1, tree2) ? (cout << ` `"Yes"` `<< endl) : ` ` ` `(cout << ` `"No"` `<< endl); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `M = 3, N = 2; ` ` ` ` ` `int` `u1[] = { 1, 1 }; ` ` ` `int` `v1[] = { 2, 3 }; ` ` ` ` ` `int` `u2[] = { 1, 1 }; ` ` ` `int` `v2[] = { 3, 2 }; ` ` ` ` ` `areMirrors(M, N, u1, v1, u2, v2); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output:

Yes

Reference: https://practice.geeksforgeeks.org/problems/check-mirror-in-n-ary-tree/0

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 contribute@geeksforgeeks.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.

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.

## Recommended Posts:

- Create a mirror tree from the given binary tree
- 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
- 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
- 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
- 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 max sum level of Binary tree divides tree into two equal sum halves
- 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 if the given binary tree has a sub-tree with equal no of 1's and 0's | Set 2
- Check if the given n-ary tree is a binary tree
- Check if a binary tree is subtree of another binary tree using preorder traversal : Iterative
- Check whether a binary tree is a full binary tree or not | Iterative Approach