Amazon Interview Question
Software Engineer / DevelopersTeam: Kindle-Periodicals
Country: India
Interview Type: In-Person
1. Do an in-order traverse:
left - self - right
and store the traversed nodes in an array.
2. Do an reversed in-order traverse:
right -self - left
and store nodes in array.
3. Compare two arrays.
This approach is O(3n).
bool Mirror(node *left,node *root)
{
if(!left && !right)
return true;
if(!left || !right)
return false;
else return Mirror(left->right,right->Left) &&Mirror(left->left,right->right))
}
bool Mirror(node *left,node *root)
{
if(!left && !right)
return true;
if(!left || !right)
return false;
else return ((left->data==right->data)&&(Mirror(left->right,right->Left)) &&(Mirror(left->left,right->right))
}
def mirrorTest(t1,t2):
- Anonymous March 28, 2012if t1 is None and t2 is None :
return True
if t1 is None || t2 is None :
return False
ileft = mirrorTest(t1.left,t2.right)
iright = mirrorTest(t1.right,t2.left)
if ileft and iright :
if t1.data == t2.data :
return True
return False
#you will call the above function
mirrorTest(root,root)