Given a Two Binary Trees, write a function that returns true if one is mirror of other, else returns false.
             
Example 1:
Input:
T1:     1     T2:     1
      /   \         /   \
     2     3       3     2
Output: 1
Example 2:
Input:
T1:     10      T2:   10
       /  \          /  \
      20  30        20  30
     /  \          /  \
    40   60       40  60
Output: 0
Your Task:
You don't need to take input. Just complete the function areMirror() that takes root node of two tree as parameter and returns true, if one is the mirror of other else returns false. (The driver's code print 1 if the returned value is true, otherwise 0)
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(Height of the Tree).
Constraints:
1 <= Number of nodes<= 10000
-1000 <= Data of a node<= 1000

Source: geeksforgeeks | Difficulty: EASY
Tags: Tree algorithms

Data structures

Starter code: #User function Template for python3

'''
# Node Class:
class Node:
    def __init__(self,val):
        self.data = val
        self.left = None
        self.right = None
'''
class Solution:
    def areMirror(self,root1,root2):
        '''
        :param root1,root2: two root of the given trees.
        :return: True False
        
        '''
        #code here

Solutions:
class Solution:

	def areMirror(self, r1, r2):
		if r1 is None and r2 is None:
			return 1
		if r1 is None or r2 is None:
			return 0
		if r1.data != r2.data:
			return 0
		l = self.areMirror(r1.left, r2.right)
		r = self.areMirror(r1.right, r2.left)
		if l and r:
			return 1
		return 0


class Solution:

	def areMirror(self, root1, root2):
		if not root1 and (not root2):
			return True
		if not root1 or not root2:
			return False
		return root1.data == root2.data and self.areMirror(root1.left, root2.right) and self.areMirror(root1.right, root2.left)


def slove(r1, r2):
	if r1 is None and r2 is None:
		return True
	if r1 is not None and r2 is not None:
		if r1.data == r2.data:
			return slove(r1.left, r2.right) and slove(r1.right, r2.left)
	return False

class Solution:

	def areMirror(self, root1, root2):
		return slove(root1, root2)


def symmetric(root1, root2, flag):
	if root1 == None and root2 == None:
		return
	if root1 != None and root2 == None:
		flag[0] = False
		return
	if root1 == None and root2 != None:
		flag[0] = False
		return
	if root1.data != root2.data:
		flag[0] = False
	symmetric(root1.left, root2.right, flag)
	symmetric(root1.right, root2.left, flag)

class Solution:

	def areMirror(self, root1, root2):
		flag = [True]
		if root1 == None and root2 == None:
			return flag[0]
		symmetric(root1, root2, flag)
		return flag[0]


class Solution:

	def areMirror(self, root1, root2):
		str1 = []
		str2 = []

		def dfs(root, s):
			if not root:
				return
			dfs(root.left, s)
			s.append(root.data)
			dfs(root.right, s)
		dfs(root1, str1)
		dfs(root2, str2)
		if str1 == str2[::-1]:
			return True
		else:
			return False


class Solution:

	def areMirror(self, root1, root2):
		if root1 is None and root2 is None:
			return True
		if root1 is None or root2 is None:
			return False
		if root1.data != root2.data:
			return False
		return self.areMirror(root1.left, root2.right) and self.areMirror(root1.right, root2.left)


class Solution:

	def areMirror(self, root1, root2):

		def mirror(p, q):
			if p == None or q == None:
				return p == q
			if p.data == q.data:
				return mirror(p.left, q.right) and mirror(p.right, q.left)
			return False
		return mirror(root1, root2)


class Solution:

	def areMirror(self, node1, node2):
		if not node1 and (not node2):
			return True
		if node1 and (not node2) or (node2 and (not node1)) or node1.data != node2.data:
			return False
		return self.areMirror(node1.left, node2.right) and self.areMirror(node1.right, node2.left)


class Solution:

	def s(self, r1, r2):
		if r1 == None and r2 != None:
			return False
		if r1 != None and r2 == None:
			return False
		if r1 == None and r2 == None:
			return True
		if r1.data != r2.data:
			return False
		l = self.s(r1.left, r2.right)
		r = self.s(r1.right, r2.left)
		return l and r

	def areMirror(self, root1, root2):
		return self.s(root1, root2)


class Solution:

	def areMirror(self, root1, root2):
		if root1 == root2 == None:
			return True
		if None in [root1, root2]:
			return False
		if root1.data != root2.data:
			return False
		return self.areMirror(root1.left, root2.right) and self.areMirror(root1.right, root2.left)


class Solution:

	def areMirror(self, root1, root2):
		(l1, l2) = ([], [])

		def trel(root, l):
			if root == None:
				return
			l += [root.data]
			if root.left == None:
				l += ['null']
			trel(root.left, l)
			if root.right == None:
				l += ['null']
			trel(root.right, l)
			return l

		def trer(root, l):
			if root == None:
				return
			l += [root.data]
			if root.right == None:
				l += ['null']
			trer(root.right, l)
			if root.left == None:
				l += ['null']
			trer(root.left, l)
			return l
		if trel(root1, l1) == trer(root2, l2):
			return True
		return False


class Solution:

	def areMirror(self, root1, root2):

		def inorder(root1):
			if root1 is not None:
				inorder(root1.left)
				a.append(root1.data)
				inorder(root1.right)

		def inorder2(root2):
			if root2 is not None:
				inorder2(root2.left)
				b.append(root2.data)
				inorder2(root2.right)
		a = []
		inorder(root1)
		b = []
		inorder2(root2)
		b.reverse()
		if a == b:
			return True
		return False


class Solution:

	def areMirror(self, root1, root2):

		def inorder1(root):
			if root == None:
				return
			else:
				inorder1(root.left)
				res1.append(root.data)
				inorder1(root.right)

		def inorder2(root):
			if root == None:
				return
			else:
				inorder2(root.left)
				res2.append(root.data)
				inorder2(root.right)
		res1 = []
		res2 = []
		inorder1(root1)
		inorder2(root2)
		res2.reverse()
		if res1 == res2:
			return 1
		else:
			return 0


class Solution:

	def areMirror(self, root1, root2, flag=1):
		if root1 is None and root2 is not None or (root1 is not None and root2 is None):
			return 0
		if root1 is None and root2 is None:
			return 1
		if root1.data != root2.data:
			return 0
		flag = self.areMirror(root1.left, root2.right)
		if flag == 0:
			return 0
		flag = self.areMirror(root1.right, root2.left)
		if flag == 0:
			return 0
		return 1


class Solution:

	def areMirror(self, root1, root2):
		if root1 == None and root2 == None:
			return 1
		elif root1 == None or root2 == None:
			return 0
		elif root1.data == root2.data:
			if self.areMirror(root1.left, root2.right) and self.areMirror(root1.right, root2.left):
				return 1
			else:
				return 0
		else:
			return 0


class Solution:

	def areMirror(self, a, b):
		if a is None and b is None:
			return True
		if a is None or b is None:
			return False
		return a.data == b.data and self.areMirror(a.left, b.right) and self.areMirror(a.right, b.left)


class Solution:

	def areMirror(self, r1, r2):
		if r1 == None and r2 == None:
			return 1
		elif r1 == None or r2 == None:
			return 0
		elif r1.data == r2.data:
			if self.areMirror(r1.left, r2.right) and self.areMirror(r1.right, r2.left):
				return 1
			else:
				return 0


class Solution:

	def areMirror(self, root1, root2):
		if root1 == None and root2 == None:
			return 1
		elif root1 == None and root2 != None:
			return 0
		elif root1 != None and root2 == None:
			return 0
		elif root1.data != root2.data:
			return 0
		elif root1.data == root2.data:
			l = self.areMirror(root1.left, root2.right)
			r = self.areMirror(root1.right, root2.left)
			return min(l, r)


class Solution:

	def areMirror(self, root1, root2):

		def mirror(root):
			if root == None:
				return
			else:
				x = root.left
				root.left = root.right
				root.right = x
				mirror(root.right)
				mirror(root.left)
		mirror(root1)

		def ans(root1, root2):
			if root1 == None and root2 == None:
				return True
			elif root1 == None and root2 != None or (root2 == None and root1 != None):
				return False
			elif root1.data == root2.data:
				return ans(root1.left, root2.left) and ans(root2.right, root2.right)
			else:
				return False
		return ans(root1, root2)


class Solution:

	def areMirror(self, root1, root2):
		if root1 == None and root2 == None:
			return 1
		if root1 == None or root2 == None:
			return 0
		if root1.data != root2.data:
			return 0
		a = self.areMirror(root1.left, root2.right)
		b = self.areMirror(root1.right, root2.left)
		return a and b


class Solution:

	def areMirror(self, root1, root2):

		def check(r, t):
			if r == None and t == None:
				return True
			if r is None or t is None:
				return False
			if r.data != t.data:
				return False
			return check(r.left, t.right) and check(r.right, t.left)
		x = check(root1, root2)
		return x


class Solution:

	def areMirror(self, r1, r2):
		if not r1 and (not r2):
			return True
		if r1 and (not r2):
			return False
		if not r1 and r2:
			return False
		return r1.data == r2.data and self.areMirror(r1.left, r2.right) and self.areMirror(r1.right, r2.left)


class Solution:

	def areMirror(self, root1, root2):
		b = []
		Solution.O(root1, b)
		for i in b:
			l = i.left
			r = i.right
			(l, r) = (r, l)
			i.left = l
			i.right = r
		a = []
		Solution.preorder(root1, a)
		c = []
		Solution.preorder(root2, c)
		return a == c

	def O(root, b):
		if root.left != None:
			Solution.O(root.left, b)
		b.append(root)
		if root.right != None:
			Solution.O(root.right, b)

	def preorder(root, x):
		x.append(root.data)
		if root.left != None:
			Solution.preorder(root.left, x)
		if root.right != None:
			Solution.preorder(root.right, x)


class Solution:

	def areMirror(self, root1, root2):
		preOrderArr1 = []
		preOrderArr2 = []
		inOrderArr1 = []
		inOrderArr2 = []

		def bothOrder(node, preOrderArr, inOrderArr):
			preOrderArr.append(node.data)
			if node.left is not None:
				bothOrder(node.left, preOrderArr, inOrderArr)
			inOrderArr.append(node.data)
			if node.right is not None:
				bothOrder(node.right, preOrderArr, inOrderArr)
			return

		def bothOrderRev(node, preOrderArr, inOrderArr):
			preOrderArr.append(node.data)
			if node.right is not None:
				bothOrderRev(node.right, preOrderArr, inOrderArr)
			inOrderArr.append(node.data)
			if node.left is not None:
				bothOrderRev(node.left, preOrderArr, inOrderArr)
			return
		bothOrder(root1, preOrderArr1, inOrderArr1)
		bothOrderRev(root2, preOrderArr2, inOrderArr2)
		if preOrderArr1 == preOrderArr2 and inOrderArr1 == inOrderArr2:
			return True
		else:
			return False


class Solution:

	def areMirror(self, root1, root2):
		return self.isMirror(root1, root2)

	def isMirror(self, t1, t2):
		if t1 == None or t2 == None:
			return t1 == t2
		if t1.data != t2.data:
			return 0
		return self.isMirror(t1.left, t2.right) and self.isMirror(t1.right, t2.left)


class Solution:

	def areMirror(self, root1, root2):
		if not root1 and (not root2):
			return True
		if root1 and (not root2):
			return False
		if not root1 and root2:
			return False
		check1 = root1.data == root2.data
		check2 = self.areMirror(root1.left, root2.right)
		check3 = self.areMirror(root1.right, root2.left)
		return all([check1, check2, check3])


class Solution:

	def __init__(self):
		self.l = []
		self.m = []
		self.x = []
		self.y = []

	def mir1(self, root1, l):
		l.append(root1.data)
		(root1.left, root1.right) = (root1.right, root1.left)
		if root1.left != None:
			self.mir1(root1.left, l)
		if root1.right != None:
			self.mir1(root1.right, l)
		return l

	def mir2(self, root2, m):
		m.append(root2.data)
		if root2.left != None:
			self.mir2(root2.left, m)
		if root2.right != None:
			self.mir2(root2.right, m)
		return m

	def areMirror(self, root1, root2):
		l = []
		m = []
		x = self.mir1(root1, l)
		y = self.mir2(root2, m)
		if x == y:
			return 1
		return 0


class Solution:

	def areMirror(self, root1, root2):
		b = []

		def inorder(root, b):
			if root != None:
				inorder(root.left, b)
				if root.data == 'N':
					pass
				else:
					b.append(root.data)
				inorder(root.right, b)
			return b
		k = inorder(root1, b)
		b = []
		k2 = inorder(root2, b)
		if len(k) == len(k2):
			for i in range(len(k)):
				if k[i] == k2[-(1 + i)]:
					continue
				else:
					return 0
			return 1
		return 0


class Solution:

	def areMirror(self, root1, root2):
		if root1 is None and root2 is None:
			return True
		if root1 and root2 and (root1.data == root2.data):
			ans1 = self.areMirror(root1.left, root2.right)
			ans2 = self.areMirror(root1.right, root2.left)
			if ans1 and ans2:
				return True
			else:
				return False
		else:
			return False


class Solution:

	def areMirror(self, root1, root2):

		def isMirror(root1, root2):
			if root1 == None and root2 == None:
				return True
			if root1 == None or root2 == None:
				return False
			if root1.data != root2.data:
				return False
			a = isMirror(root1.left, root2.right)
			b = isMirror(root1.right, root2.left)
			return a and b
		return isMirror(root1, root2)


class Solution:

	def areMirror(self, root1, root2):
		p = root1
		q = root2

		def mirrorTrees(p, q):
			if p == None and q == None:
				return True
			if p == None or q == None:
				return False
			return p.data == q.data and mirrorTrees(p.left, q.right) and mirrorTrees(p.right, q.left)
		value = mirrorTrees(p, q)
		return value


class Solution:

	def areMirror(self, root1, root2):
		if root1 == None and root2 == None:
			return True
		elif root1 != None and root2 != None and (root1.data == root2.data):
			return self.areMirror(root1.left, root2.right) and self.areMirror(root1.right, root2.left)
		else:
			return False


class Solution:

	def areMirror(self, root1, root2):

		def helper(root, ans):
			if root:
				helper(root.left, ans)
				ans.append(root.data)
				helper(root.right, ans)
				return ans
		if helper(root1, []) == helper(root2, [])[::-1]:
			return True
		return False


class Solution:

	def areMirror(self, root1, root2):

		def helper1(x):
			l = []
			if x:
				l += helper1(x.left)
				l.append(x.data)
				l += helper1(x.right)
			return l

		def helper2(y):
			m = []
			if y:
				m += helper2(y.right)
				m.append(y.data)
				m += helper2(y.left)
			return m
		a = helper1(root1)
		b = helper2(root2)
		if a == b:
			return True
		return False