© Parineeth M R

** Question 13. Convert a binary tree into its mirror image**

An example of a binary tree and its mirror image is shown below.

So to convert the binary tree to its mirror image, the left child and the right child of each node in the tree should be swapped

## C/C++

/* cur_node: current node of the tree whose mirror image should be computed */ void compute_mirror_image(struct node *cur_node) { struct node *temp_node; if (cur_node) { /*Swap the left child and right child of the current node*/ temp_node = cur_node->left; cur_node->left = cur_node->right; cur_node->right = temp_node; /*Recursively compute the mirror image */ compute_mirror_image(cur_node->left); compute_mirror_image(cur_node->right); } }

## Java

/* curNode: current node of the tree whose mirror image should be computed */ public static void computeMirrorImage(TreeNode curNode) { if (curNode != null) { /*Swap the left child and right child of the current node*/ TreeNode tempNode = curNode.left; curNode.left = curNode.right; curNode.right = tempNode; /*Recursively compute the mirror image */ computeMirrorImage(curNode.left); computeMirrorImage(curNode.right); } }

## Python

@staticmethod #cur_node: current node of the tree whose mirror image should be computed def compute_mirror_image(cur_node): if (cur_node) : #Swap the left child and right child of the current node cur_node.left, cur_node.right = cur_node.right, cur_node.left #Recursively compute the mirror image TreeNode.compute_mirror_image(cur_node.left) TreeNode.compute_mirror_image(cur_node.right)