# Convert binary tree to mirror tree in python

Python program for Convert binary tree to mirror tree. Here more information.

``````#  Python 3 program for
#  Convert binary tree to it's mirror tree

#  Binary Tree Node
class TreeNode :
def __init__(self, data) :
#  Set node value
self.data = data
self.left = None
self.right = None

class BinaryTree :
def __init__(self) :
self.root = None

#  Display inorder element
def inorder(self, node) :
if (node != None) :
self.inorder(node.left)
#  Print node value
print("", node.data, end = " ")
self.inorder(node.right)

#  Convert binary tree into mirror tree
def mirrorTree(self, node) :
if (node != None) :
self.mirrorTree(node.left)
self.mirrorTree(node.right)
#  Change left and right child
temp = node.left
node.left = node.right
node.right = temp

def main() :
#  Create new tree
tree = BinaryTree()
#   Binary Tree
#    -----------------------
#         1
#       /   \
#      6     8
#     / \   / \
#    2   3 4   5
#   /           \
#  19            10
tree.root = TreeNode(1)
tree.root.left = TreeNode(6)
tree.root.left.left = TreeNode(2)
tree.root.right = TreeNode(8)
tree.root.right.right = TreeNode(5)
tree.root.right.left = TreeNode(4)
tree.root.left.right = TreeNode(3)
tree.root.left.left.left = TreeNode(19)
tree.root.right.right.right = TreeNode(10)
print("\n Before convert", end = "")
print("\n In-order Data : ", end = "")
tree.inorder(tree.root)
#  Transform into mirror tree
tree.mirrorTree(tree.root)
#    Mirror Tree
#    --------------
#         1
#       /   \
#      8     6
#     / \   / \
#    5   4 3   2
#   /           \
#  10            19
print("\n After convert", end = "")
print("\n In-order Data : ", end = "")
tree.inorder(tree.root)

if __name__ == "__main__": main()``````

Output

`````` Before convert
In-order Data :  19  2  6  3  1  4  8  5  10
After convert
In-order Data :  10  5  8  4  1  3  6  2  19``````

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.