## Flipkart Interview Question

Software Engineer / DevelopersTime Complexity

O(logn) -> searching that node

O(logn) -> moving for the mirror

total : O(logn)

Space Complexity : O(logn)

Node getMirrorNode(Node currNode , node mirrorNode , int data)

{

if(currNode == null || mirrorNode==null)

return null;

if(currNode->value == data)

return mirrorNode;

Node tmp;

tmp = getMirrorNode(currNode->left , mirrorNode->right , data);

if(tmp != null)

return tmp;

tmp = getMirrorNode(currNode->right , mirrorNode->left , data);

if(tmp != null)

return tmp;

}

I think mirror node of a node would always be its other sibling.

i.e. if this node is a left child of its parent then the mirror node would be the right child of its parent.

so..

if(node->right->data == data) return node->left;

else if (node->left->data == data) return node->right;

else{ tmp = getMirrorNode(node->left, data); if (tmp!= null) return tmp;

tmp = getMirrorNode(node->right, data); if (tmp!= null) return tmp;

}

No mirror can not be put in the middle of any node be it root or another node.

Tree Original Mirror Reflection

9 | 9

4 16 | 16 4

3 5 12 | 12 5 3

No mirror can not be put in the middle of any node be it root or another node.

Tree Original Mirror Reflection

```
9 | 9
4 16 | 16 4
3 5 12 | 12 5 3
```

So that is why the mirror of any given node is its sibling. Left or Right as applicable.

@Loony: Check again, mirror node of 5 is 3 and not 12... 12's mirror node is empty or may be you try to draw a balanced tree with 3 nodes and see its reflection in mirror,

If sibling is consider as mirror node. wouldn't this Question be like "find the sibling of given input ? " LOL

Yes that is catch, I mean the question asked in a different way..

I may be wrong please some one help me if so

No, by the mirror node we mean .. If we compare the path from root node to the two nodes .. the path should be exactly opposite at each turn ... i.e. for every right move in the first path you should have a left move in the second path.

And that, in fact, is the answer .. find path and reverse it accordingly to find the mirror node.

No, by the mirror node we mean .. If we compare the path from root node to the two nodes .. the path should be exactly opposite at each turn ... i.e. for every right move in the first path you should have a left move in the second path.

And that, in fact, is the answer .. find path and reverse it accordingly to find the mirror node.

public Node mirrorNode(Node head, int data){

if(head==null)

return null;

Node tmp = head;

Node mirror = head;

while(tmp!=null){

if(data==tmp.data)

return mirror;

if(data<tmp.data){

tmp = tmp.left

mirror=mirror.right

}else if(data>tmp.data){

tmp = tmp.right;

miror= mirror.left

}

}

return null;

}

Node * mirror(Node *n)

{

Node *temp = n;

while(n!=NULL && n->value!=value && temp!=NULL)

{

if(n->value < value)

{

n = n->right;

temp = temp->left;

}

else

{

n = n->left;

temp = temp->right;

}

}

if(temp!=NULL && n!=NULL)

return temp;

else

return NULL;

}

@vicky, iterative solution is better than recursive

This is how I will do it

- DashDash July 18, 20101) traverse till the node and use and array say if we move left store 0 and move right store 1

2) Now again traverse the tree, reading the array, if 0 then move right else move left

this will give us the mirror.