JIGYASA
Would you like to react to this message? Create an account in a few clicks or log in to continue.
JIGYASA

An online placement forum.


You are not connected. Please login or register

Mirroring a binary tree.

2 posters

Go down  Message [Page 1 of 1]

1Mirroring a binary tree. Empty Mirroring a binary tree. Sun Sep 12, 2010 6:27 am

Lucifer


Admin

Write a function which takes a binary tree and returns a mirror image of the same.

For ex-
Code:

  Input              Output

    1                  1
  2  3              3    2

https://jigyasa.forumotion.com

2Mirroring a binary tree. Empty Re: Mirroring a binary tree. Tue Sep 21, 2010 5:41 am

codecrker



struct node
{
int data;
struct node * lChild;
struct node * rChild;
};


void mirror (node *root)
{
if (root)
{
node *temp = NULL;
// Swap the children..
temp = root->rChild;
root->rChild = root->lChild;
root->lChild = temp;

// Mirror the children..
mirror(root->lChild);
mirror(root->rChild);
}
}

Back to top  Message [Page 1 of 1]

Permissions in this forum:
You cannot reply to topics in this forum