# Check if two trees are Mirror | Set 2

• Difficulty Level : Easy
• Last Updated : 24 Jun, 2021

Given two Binary Trees, returns true if two trees are mirror of each other, else false.

Mirror Tree :

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Approach :
Find the inorder traversal of both the Binary Trees, and check whether one traversal is reverse of another or not. If they are reverse of each other then the trees are mirror of each other, else not.

## C++

 `// C++ code to check two binary trees are``// mirror.``#include``using` `namespace` `std;` `struct` `Node``{``    ``int` `data;``    ``Node* left, *right;``};` `// inorder traversal of Binary Tree``void` `inorder(Node *n, vector<``int``> &v)``{``    ``if` `(n->left != NULL)``    ``inorder(n->left, v);       ``    ``v.push_back(n->data);   ``    ``if` `(n->right != NULL)``    ``inorder(n->right, v);``}` `// Checking if binary tree is mirror``// of each other or not.``bool` `areMirror(Node* a, Node* b)``{``  ``if` `(a == NULL && b == NULL)``    ``return` `true``;   ``  ``if` `(a == NULL || b== NULL)``    ``return` `false``;`` ` `  ``// Storing inorder traversals of both``  ``// the trees.``  ``vector<``int``> v1, v2;``  ``inorder(a, v1);``  ``inorder(b, v2);` `  ``if` `(v1.size() != v2.size())``     ``return` `false``;` `  ``// Comparing the two arrays, if they``  ``// are reverse then return 1, else 0``  ``for` `(``int` `i=0, j=v2.size()-1; j >= 0;``                             ``i++, j--)``    ` `      ``if` `(v1[i] != v2[j])``        ``return` `false``;   ``    ` `  ``return` `true``;``}` `// Helper function to allocate a new node``Node* newNode(``int` `data)``{``  ``Node* node = ``new` `Node;``  ``node->data  = data;``  ``node->left  =  node->right  = NULL;``  ` `  ``return``(node);``}`` ` `// Driver code``int` `main()``{``  ``Node *a = newNode(1);``  ``Node *b = newNode(1);``  ` `  ``a -> left = newNode(2);``  ``a -> right = newNode(3);``  ``a -> left -> left  = newNode(4);``  ``a -> left -> right = newNode(5);`` ` `  ``b -> left = newNode(3);``  ``b -> right = newNode(2);``  ``b -> right -> left = newNode(5);``  ``b -> right -> right = newNode(4);`` ` `  ``areMirror(a, b)? cout << ``"Yes"` `: cout << ``"No"``;`` ` `  ``return` `0;``}`

## Python3

 `# Python3 code to check two binary trees are``# mirror.``class` `Node:` `    ``def` `__init__(``self``, data):``        ``self``.data ``=` `data``        ``self``.left ``=` `None``        ``self``.right ``=` `None``    ` `# inorder traversal of Binary Tree``def` `inorder(n, v):` `    ``if` `(n.left !``=` `None``):``        ``inorder(n.left, v);       ``    ``v.append(n.data);   ``    ``if` `(n.right !``=` `None``):``        ``inorder(n.right, v);` `# Checking if binary tree is mirror``# of each other or not.``def` `areMirror(a, b):` `    ``if` `(a ``=``=` `None` `and` `b ``=``=` `None``):``        ``return` `True``;   ``    ``if` `(a ``=``=` `None` `or` `b``=``=` `None``):``        ``return` `False``;` `    ``# Storing inorder traversals of both``    ``# the trees.``    ``v1 ``=` `[]``    ``v2 ``=` `[]``    ``inorder(a, v1);``    ``inorder(b, v2);` `    ``if` `(``len``(v1) !``=` `len``(v2)):``       ``return` `False``;` `    ``# Comparing the two arrays, if they``    ``# are reverse then return 1, else 0``    ``i ``=` `0``    ``j ``=` `len``(v2) ``-` `1`` ` `    ``while` `j >``=` `0``:``    ` `        ``if` `(v1[i] !``=` `v2[j]):``            ``return` `False``        ``i``+``=``1``        ``j``-``=``1``    ` `    ``return` `True``;` `# Helper function to allocate a new node``def` `newNode(data):``    ``node ``=` `Node(data)``    ``return` `node``     ` `# Driver code``if` `__name__``=``=``"__main__"``:``    ``a ``=` `newNode(``1``);``    ``b ``=` `newNode(``1``);``    ` `    ``a.left ``=` `newNode(``2``);``    ``a.right ``=` `newNode(``3``);``    ``a.left.left  ``=` `newNode(``4``);``    ``a.left.right ``=` `newNode(``5``);` `    ``b.left ``=` `newNode(``3``);``    ``b.right ``=` `newNode(``2``);``    ``b.right.left ``=` `newNode(``5``);``    ``b.right.right ``=` `newNode(``4``);` `    ``if` `areMirror(a, b):``        ``print``(``"Yes"``)``    ``else``:``        ``print``(``"No"``)` `# This code is contributed by rutvik_56`

## C#

 `// C# code to check two binary trees are``// mirror.``using` `System;``using` `System.Collections;``using` `System.Collections.Generic;` `class` `GFG{``    ` `class` `Node``{``    ``public` `int` `data;``    ``public` `Node left, right;``};`` ` `// inorder traversal of Binary Tree``static` `void` `inorder(Node n, ``ref` `List<``int``> v)``{``    ``if` `(n.left != ``null``)``        ``inorder(n.left, ``ref` `v);``        ` `    ``v.Add(n.data);   ``    ` `    ``if` `(n.right != ``null``)``        ``inorder(n.right, ``ref` `v);``}`` ` `// Checking if binary tree is mirror``// of each other or not.``static` `bool` `areMirror(Node a, Node b)``{``    ``if` `(a == ``null` `&& b == ``null``)``        ``return` `true``;   ``    ``if` `(a == ``null` `|| b == ``null``)``        ``return` `false``;``        ` `    ``// Storing inorder traversals of both``    ``// the trees.``    ``List<``int``> v1 = ``new` `List<``int``>();``    ``List<``int``> v2 = ``new` `List<``int``>();``    ` `    ``inorder(a, ``ref` `v1);``    ``inorder(b, ``ref` `v2);``    ` `    ``if` `(v1.Count != v2.Count)``        ``return` `false``;``    ` `    ``// Comparing the two arrays, if they``    ``// are reverse then return 1, else 0``    ``for``(``int` `i = 0, j = v2.Count - 1; j >= 0;``            ``i++, j--)``    ` `    ``if` `(v1[i] != v2[j])``        ``return` `false``;   ``    ` `    ``return` `true``;``}`` ` `// Helper function to allocate a new node``static` `Node newNode(``int` `data)``{``    ``Node node = ``new` `Node();``    ``node.data = data;``    ``node.left = node.right = ``null``;``    ``return``(node);``}``  ` `// Driver code``static` `void` `Main(``string` `[]args)``{``    ``Node a = newNode(1);``    ``Node b = newNode(1);``    ` `    ``a.left = newNode(2);``    ``a.right = newNode(3);``    ``a.left.left  = newNode(4);``    ``a.left.right = newNode(5);``    ` `    ``b.left = newNode(3);``    ``b.right = newNode(2);``    ``b.right.left = newNode(5);``    ``b.right.right = newNode(4);``    ` `    ``if` `(areMirror(a, b))``    ``{``        ``Console.Write(``"Yes"``);``    ``}``    ``else``    ``{``        ``Console.Write(``"No"``);``    ``}``}``}` `// This code is contributed by pratham76`

## Javascript

 ``

Output:

`Yes`

This article is contributed by Anuj Chauhan. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.