Given a Binary Tree, convert it into its mirror.
             
Example 1:
Input:
      1
    /  \
   2    3
Output: 3 1 2
Explanation: The tree is
   1    (mirror)  1
 /  \    =>      /  \
2    3          3    2
The inorder of mirror is 3 1 2
Example 2:
Input:
      10
     /  \
    20   30
   /  \
  40  60
Output: 30 10 60 20 40
Explanation: The tree is
      10               10
    /    \  (mirror) /    \
   20    30    =>   30    20
  /  \                   /   \
 40  60                 60   40
The inroder traversal of mirror is
30 10 60 20 40.
Your Task:
Just complete the function mirror() that takes node as paramter  and convert it into its mirror. The printing is done by the driver code only.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(Height of the Tree).
Constraints:
1 ≤ Number of nodes ≤ 10^{5}
1 ≤ Data of a node ≤ 10^{5}

Source: geeksforgeeks | Difficulty: EASY
Tags: Tree algorithms

Data structures

Starter code: #User function Template for python3

'''
class Node:
    def _init_(self, val):
        self.right = None
        self.data = val
        self.left = None
'''
# your task is to complete this function

class Solution:
    #Function to convert a binary tree into its mirror tree.
    def mirror(self,root):
        # Code here

Solutions:
class Solution:

	def mirror(self, root):

		def _mirror(node):
			if node is None:
				return
			(node.right, node.left) = (node.left, node.right)
			_mirror(node.right)
			_mirror(node.left)
		_mirror(root)


class Solution:

	def mirror(self, root):
		if root == None:
			return root
		self.mirror(root.left)
		self.mirror(root.right)
		temp = root.left
		root.left = root.right
		root.right = temp


class Solution:

	def mirror(self, root):
		if not root:
			return
		self.mirror(root.left)
		self.mirror(root.right)
		(r, l) = (None, None)
		if root.right:
			r = root.right
		if root.left:
			l = root.left
		(root.left, root.right) = (r, l)


class Solution:

	def invert(self, root):
		if root == None:
			return root
		(root.left, root.right) = (root.right, root.left)
		self.invert(root.left)
		self.invert(root.right)
		return root

	def mirror(self, root):
		return self.invert(root)


class Solution:

	def _solve(self, node):
		if not node:
			return
		if not node.left and (not node.right):
			return
		self._solve(node.left)
		self._solve(node.right)
		temp_node = node.left
		node.left = node.right
		node.right = temp_node

	def mirror(self, root):
		self._solve(root)


class Solution:

	def mirror(self, root):
		if not root:
			return
		tempLeft = root.left
		tempRight = root.right
		if tempRight:
			root.left = self.mirror(tempRight)
		else:
			root.left = None
		if tempLeft:
			root.right = self.mirror(tempLeft)
		else:
			root.right = None
		return root


class Solution:

	def solve(self, root):
		if root == None:
			return
		self.solve(root.left)
		self.solve(root.right)
		(root.left, root.right) = (root.right, root.left)

	def mirror(self, root):
		self.solve(root)
		return root


class Solution:

	def mirror(self, root):
		if not root:
			return
		left = root.left
		right = root.right
		root.left = right
		root.right = left
		self.mirror(root.left)
		self.mirror(root.right)
		return


class Solution:

	def mirror(self, node):
		if node is None:
			return
		self.mirror(node.left)
		self.mirror(node.right)
		temp = node.left
		node.left = node.right
		node.right = temp
		return node


class Solution:

	def mirror(self, root):
		if root == None:
			return -1
		self.mirror(root.left)
		self.mirror(root.right)
		(root.left, root.right) = (root.right, root.left)


class Solution:

	def mirror(self, root):
		if root:
			(root.left, root.right) = (root.right, root.left)
			self.mirror(root.left)
			self.mirror(root.right)


class Solution:

	def mirror(self, root):
		if root is not None:
			left = root.left
			right = root.right
			root.left = self.mirror(right)
			root.right = self.mirror(left)
		return root


class Solution:

	def mirror(self, root):
		if root == None:
			return root
		leftree = root.right
		rightree = root.left
		root.left = self.mirror(leftree)
		root.right = self.mirror(rightree)
		return root


class Solution:

	def mirror(self, root):
		COUNT = [10]

		def print2DUtil(root, space):
			if root == None:
				return
			space += COUNT[0]
			print2DUtil(root.right, space)
			print()
			for i in range(COUNT[0], space):
				print(end=' ')
			print(root.data)
			print2DUtil(root.left, space)

		def print2D(root):
			print2DUtil(root, 0)

		def mirror_util(root):
			if not root or (not root.left and (not root.right)):
				return
			(root.left, root.right) = (root.right, root.left)
			mirror_util(root.left)
			mirror_util(root.right)
		mirror_util(root)


class Solution:

	def mirror(self, root):
		if not root:
			return
		q = [root]
		while q:
			cur = q[0]
			q.pop(0)
			temp = cur.left
			cur.left = cur.right
			cur.right = temp
			if cur.left:
				q.append(cur.left)
			if cur.right:
				q.append(cur.right)
		return root


class Solution:

	def mirror(self, root):
		return self.mirrorTree(root)

	def mirrorTree(self, node):
		if not node:
			return node
		left = self.mirrorTree(node.left)
		right = self.mirrorTree(node.right)
		node.left = right
		node.right = left
		return node


class Solution:

	def mirror(self, root):
		if root == None:
			return
		else:
			self.mirror(root.left)
			self.mirror(root.right)
			(root.left, root.right) = (root.right, root.left)


class Solution:

	def mirror(self, root):
		q = []
		q.append(root)
		while q:
			n = q.pop(0)
			if n.right:
				q.append(n.right)
			if n.left:
				q.append(n.left)
				(n.left, n.right) = (n.right, n.left)
			elif n.right:
				(n.left, n.right) = (n.right, n.left)


class Solution:

	def mirror(self, root):
		if root is None:
			return
		q = []
		q.append(root)
		while q:
			n = q.pop(0)
			if n.right:
				q.append(n.right)
			if n.left:
				q.append(n.left)
				(n.left, n.right) = (n.right, n.left)
			elif n.right:
				(n.left, n.right) = (n.right, n.left)


class Solution:

	def swap(self, root):
		tk = root.left
		root.left = root.right
		root.right = tk

	def mirror(self, root):
		if root is None:
			return root
		q = []
		q.append(root)
		while q:
			temp = q.pop(0)
			self.swap(temp)
			if temp.left:
				q.append(temp.left)
			if temp.right:
				q.append(temp.right)
		return root


class Solution:

	def mirror(self, root):

		def turn(root):
			if not root:
				return root
			lt = turn(root.left)
			rt = turn(root.right)
			(root.left, root.right) = (rt, lt)
			return root
		return turn(root)


class Solution:

	def trace(this, node):
		if node.left == None:
			if node.right != None:
				node.left = node.right
				node.right = None
				this.trace(node.left)
		elif node.right == None:
			if node.left != None:
				node.right = node.left
				node.left = None
				this.trace(node.right)
		else:
			temp = node.left
			node.left = node.right
			node.right = temp
			this.trace(node.left)
			this.trace(node.right)

	def mirror(self, root):
		self.trace(root)


class Solution:

	def mirr(self, root):
		if root is None:
			return
		else:
			temp = root
			self.mirr(root.left)
			self.mirr(root.right)
			temp = root.left
			root.left = root.right
			root.right = temp

	def mirror(self, root):
		self.mirr(root)
		return root


def swap(curr):
	if curr is None:
		return
	swap(curr.left)
	swap(curr.right)
	dummy = Node(0)
	dummy = curr.left
	curr.left = curr.right
	curr.right = dummy

class Solution:

	def mirror(self, root):
		swap(root)


class Solution:

	def mirror(self, root):

		def h(root):
			if root == None:
				return root
			(root.left, root.right) = (h(root.right), h(root.left))
			return root
		return h(root)


class Solution:

	def mirror(self, root):
		a = root.right
		b = root.left
		root.right = b
		root.left = a
		if a != None:
			self.mirror(a)
		if b != None:
			self.mirror(b)