Given a Binary Tree. Check whether it is Symmetric or not, i.e. whether the binary tree is a Mirror image of itself or not.
Example 1:
Input:
         5
       /   \
      1     1
     /       \
    2         2
Output: True
Explanation: Tree is mirror image of
itself i.e. tree is symmetric
Example 2:
Input:
         5
       /   \
      10     10
     /  \     \
    20  20     30
Output: False
Your Task:
You don't need to read input or print anything. Your task is to complete the function isSymmetric() which takes the root of the Binary Tree as its input and returns True if the given Binary Tree is the same as the Mirror image of itself. Else, it returns False.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(Height of the Tree).
Constraints:
0<=Number of nodes<=100

Source: geeksforgeeks | Difficulty: EASY
Tags: Tree algorithms

Data structures

Starter code: #User function Template for python3



class Solution:

    # return true/false denoting whether the tree is Symmetric or not

    def isSymmetric(self, root):

        # Your Code Here

Solutions:
class Solution:

	def isSymmetric(self, root):
		return self.Symmetric(root, root)

	def Symmetric(self, 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
		else:
			ls = self.Symmetric(root1.left, root2.right)
			rs = self.Symmetric(root1.right, root2.left)
			return ls and rs


class Solution:

	def checkSym(self, root1, root2):
		if root1 == None or root2 == None:
			return root1 == root2
		return root1.data == root2.data and self.checkSym(root1.left, root2.right) and self.checkSym(root1.right, root2.left)

	def isSymmetric(self, root):
		if root is None:
			return True
		return self.checkSym(root.left, root.right)


class Solution:

	def isSymmetric(self, root):

		def symmetry(left_rt, right_rt):
			if left_rt is None or right_rt is None:
				if left_rt == right_rt:
					return True
				else:
					return False
			return left_rt.data == right_rt.data and symmetry(left_rt.left, right_rt.right) and symmetry(left_rt.right, right_rt.left)
		if root is None:
			return True
		else:
			return symmetry(root.left, root.right)


class Solution:

	def isSymmetric(self, root):

		def answer(root1, root2):
			if root1 == None and root2 == None:
				return True
			if root1 != None and root2 == None:
				return False
			if root2 != None and root1 == None:
				return False
			x = root1.data == root2.data
			y = answer(root1.left, root2.right)
			z = answer(root1.right, root2.left)
			return x and y and z
		if root == None:
			return True
		return answer(root.left, root.right)


class Solution:

	def isSymmetric(self, root):
		q = []
		q.append(root)
		while q:
			l = []
			for x in range(0, len(q)):
				cur = q[0]
				q.remove(cur)
				if cur:
					if cur.left:
						l.append(cur.left.data)
						q.append(cur.left)
					if not cur.left:
						l.append(None)
					if cur.right:
						l.append(cur.right.data)
						q.append(cur.right)
					else:
						l.append(None)
			if l[:len(l) // 2] != l[len(l) // 2:][::-1]:
				return False
		return True


class Solution:

	def isSymmetric(self, root):

		def isSymmetricc(p, q):
			if not p or not q:
				return p == q
			return p.data == q.data and isSymmetricc(p.left, q.right) and isSymmetricc(p.right, q.left)
		return isSymmetricc(root, root)


class Solution:

	def isSymmetric(self, root):
		if root is None:
			return True
		else:
			return self.isMirror(root.left, root.right)

	def isMirror(self, p1, p2):
		if p1 is None and p2 is None:
			return True
		elif p1 is None or p2 is None:
			return False
		else:
			return p1.data == p2.data and self.isMirror(p1.left, p2.right) and self.isMirror(p1.right, p2.left)


class Solution:

	def issy(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.issy(root1.left, root2.right) and self.issy(root1.right, root2.left)

	def isSymmetric(self, root):
		return self.issy(root, root)


class Solution:

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

	def isSymmetric(self, root):
		return self.check(root, root)


class Solution:

	def isSymmetric(self, root):
		return root == None or self.isSymmetricHelp(root.left, root.right)

	def isSymmetricHelp(self, left, right):
		if left is None or right is None:
			return left == right
		if left.data != right.data:
			return False
		return self.isSymmetricHelp(left.left, right.right) and self.isSymmetricHelp(left.right, right.left)


class Solution:

	def isSymmetric(self, root):
		if root == None:
			return True
		return find(root.left, root.right)

def find(left, right):
	if left == None and right == None:
		return True
	if left == None or right == None:
		return False
	if left.data != right.data:
		return False
	return find(left.left, right.right) and find(left.right, right.left)


class Solution:

	def isSymmetric(self, root):
		return self.isMirror(root, root)

	def isMirror(self, node1, node2):
		if node1 is None and node2 is None:
			return True
		if node1 and node2 and (node1.data == node2.data):
			return self.isMirror(node1.left, node2.right) and self.isMirror(node1.right, node2.left)
		return False


class Solution:

	def isSymmetric(self, root):
		return self.Symmetric(root, root)

	def Symmetric(self, node1, node2):
		if node1 == None and node2 == None:
			return True
		if node1 == None or node2 == None:
			return False
		if node1.data != node2.data:
			return False
		else:
			ls = self.Symmetric(node1.left, node2.right)
			rs = self.Symmetric(node1.right, node2.left)
			return ls and rs


class Solution:

	def isSameTree(self, p, q):
		while p and q:
			if p.data == q.data:
				return True and self.isSameTree(p.left, q.right) and self.isSameTree(p.right, q.left)
			else:
				return False
		if p and (not q) or (q and (not p)):
			return False
		elif not p and (not q):
			return True

	def isSymmetric(self, root):
		if root is None:
			return True
		else:
			t = self.isSameTree(root.left, root.right)
		return t


class Solution:

	def isSymmetric(self, root):
		return self.isMirror(root, root)

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


class Solution:

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

	def isSymmetric(self, root):
		if root == None:
			return True
		return self.Ismirror(root.left, root.right)


class Solution:

	def isSymmetric(self, root):
		if root is None:
			return True

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


class Solution:

	def isSymmetric(self, root):

		def _isSymmetric(root, level, res):
			val = root.data if root else None
			if level < len(res):
				res[level].append(val)
			else:
				res.append([val])
			if not root:
				return
			_isSymmetric(root.left, level + 1, res)
			_isSymmetric(root.right, level + 1, res)
		res = []
		_isSymmetric(root, 0, res)
		for i in res:
			if i != i[::-1]:
				return False
		return True


class Solution:

	def isSymmetric(self, root):
		if root == None:
			return True

		def check_equal(root1, root2):
			if not root1 and (not root2):
				return True
			elif root1 and root2:
				return root1.data == root2.data and check_equal(root1.left, root2.right) and check_equal(root1.right, root2.left)
			return False
		return check_equal(root.left, root.right)


class Solution:

	def isSymmetric(self, root):

		def check(tree1, tree2):
			if not tree1 and (not tree2):
				return True
			elif tree1 and tree2:
				return tree1.data == tree2.data and check(tree1.left, tree2.right) and check(tree1.right, tree2.left)
			return False
		return not root or check(root.left, root.right)


class Solution:

	def isSymmetric(self, root):
		if root is None:
			return True

		def levelOrder(root, ans):
			if root is None:
				return
			queue = []
			queue.append(root)
			while len(queue) > 0:
				level = []
				for i in range(len(queue)):
					root = queue[0]
					queue.pop(0)
					if root.left is not None:
						queue.append(root.left)
					if root.right is not None:
						queue.append(root.right)
					level.append(root.data)
				ans.append(level)
			return ans
		l1 = []
		l1.append([root.data])
		levelOrder(root.left, l1)
		l2 = []
		l2.append([root.data])
		levelOrder(root.right, l2)
		l3 = []
		for i in range(len(l2)):
			l3.append(l2[i][::-1])
		if l1 == l3:
			return True
		return False


class Solution:

	def isSymmetric(self, root):
		if root is None:
			return True

		def levelOrderLeft(root, ans):
			if root is None:
				return
			queue = []
			queue.append(root)
			while len(queue) > 0:
				level = []
				for i in range(len(queue)):
					root = queue[0]
					queue.pop(0)
					if root.left:
						queue.append(root.left)
					if root.right:
						queue.append(root.right)
					level.append(root.data)
				ans.append(level)
			return ans
		l1 = []
		l1.append(root.data)
		levelOrderLeft(root.left, l1)

		def levelOrderRight(root, ans):
			if root is None:
				return
			queue = []
			queue.append(root)
			while len(queue) > 0:
				level = []
				for i in range(len(queue)):
					root = queue[0]
					queue.pop(0)
					if root.left:
						queue.append(root.left)
					if root.right:
						queue.append(root.right)
					level.append(root.data)
				ans.append(level[::-1])
			return ans
		l2 = []
		l2.append(root.data)
		levelOrderRight(root.right, l2)
		if len(l1) == 0 and len(l2) == 0:
			return True
		if l1 == l2:
			return True
		return False


class Solution:

	def isSymmetric(self, root):

		def checksym(self, left, right):
			if (left == None) ^ (right == None):
				return 0
			if left == None and right == None:
				return 1
			if left.data == right.data:
				return checksym(self, left.left, right.right) and checksym(self, left.right, right.left)
		if root == None:
			return 1
		else:
			return checksym(self, root.left, root.right)


class Solution:

	def isSymmetric(self, root):

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