Suppose there is a tree:
1
/ \\
2 3
/ \\
4 5
Then the mirror image will
struct node *MirrorOfBinaryTree( struct node *root)
{ struct node *temp;
if(root)
{
MirrorOfBinaryTree(root->left);
MirrorOfBinaryTree(root->right);
/*swap the pointers in this node*/
temp=root->right;
root->right=root->left;;
root->left=temp;
}
return root;
}
Time complexity: O(n) Space complexity: O(n)
Here is my function. Do suggest if any better solution:
void mirrorimage(struct node *p)
{
struct node *q;
if(p!=NULL)
{
q=swaptrs(&p);
p=q;
mirrorimage(p->left);
mirrorimage(p->right);
}
}
struct node* swaptrs(struct node **p)
{
struct node *temp;
temp=(*p)->left;
(*p)->left=(*p)->right;
(*p)->right=temp;
return (*p);
}
void swap_node(node n) {
if(n != null) {
node tmp = n.left;
n.left = n.right;
n.right = tmp;
swap_node(n.left);
swap_node(n.right);
}
}
swap_node(root);
TreeNode * mirror(TreeNode *node){
if(node==NULL){
return NULL;
}else{
TreeNode *temp=node->left;
node->left=mirror(node->right);
node->right=mirror(temp);
return node;
}
}
Here's a non-recursive way of doing it in python using queues. The Queue class may be initialised like this:
class Queue(object):
def __init__(self):
self.items = []
def enqueue(self, item):
self.items.insert(0, item)
def dequeue(self):
if not self.is_empty():
return self.items.pop()
def is_empty(self):
return len(self.items) == 0
def peek(self):
if not self.is_empty():
return self.items[-1]
def __len__(self):
return self.size()
def size(self):
return len(self.items)
The class which represents a node:
class Node:
def __init__(self, data):
self.left = None
self.right = None
self.val = data
And here's the method along with traversal example which does the task:
class BinaryTree(object):
def __init__(self, root):
self.root = Node(root)
def inorder(self, start, traversal):
if start:
traversal = self.inorder(start.left, traversal)
traversal += f"{start.val} "
traversal = self.inorder(start.right, traversal)
return traversal
def mirror_tree_iterative(root):
if root is None:
return
q = Queue()
q.enqueue(root)
while not q.is_empty():
curr = q.peek()
q.dequeue()
curr.left, curr.right = curr.right, curr.left
if curr.left:
q.enqueue(curr.left)
if curr.right:
q.enqueue(curr.right)
tree = BinaryTree(1)
tree.root.left = Node(2)
tree.root.right = Node(3)
tree.root.left.left = Node(4)
tree.root.left.right = Node(5)
tree.root.right.left = Node(6)
print(tree.inorder(tree.root, ''))
mirror_tree_iterative(tree.root)
print(tree.inorder(tree.root, ''))
Well, this ques has got a lot of answers.I am posting an iterative version for this which is quite easy to understand .This uses level order Traversal.
//Function for creating Binary Tree
//Assuming *root for original tree and *root2 for mirror tree
//are declared globally
void insert(int data)
{
struct treenode* temp;
if(root2 == NULL)
{
root2 = createNode(data);
return;
}
else{
queue<treenode*> q;
q.push(root2);
while(!q.empty())
{
temp = q.front();
q.pop();
if(temp->left)
q.push(temp->left);
else{
temp->left = createNode(data);
return;
}
if(temp->right)
{
q.push(temp->right);
}
else{
temp -> right = createNode(data);
return;
}
}
}
}
//Iterative version for Mirror of a Binary Tree
void mirrorBinaryTree()
{
struct treenode *front;
queue<treenode*> q;
q.push(root);
while(!q.empty())
{
front = q.front();
insert(front->data);
q.pop();
if(front->right)
q.push(front->right);
if(front->left)
q.push(front->left);
}
}