## Amazon Interview Question

Consultants**Country:**United States

**Interview Type:**Phone Interview

Please disregard my previous post. I didn't see that the screen scrolls to the right. This solution works! +1 from me.

main()

{

p=root->leftchild

q=root->rightchild

identical(&p,&q)

}

identical(struct btreenode*a,struct btreenode*b)

{

if((a==null&&b==null)

return true;

else if (a!=null&&b!=null)

{

return (a->data==b->data&& identical(a->left,b->right)&&identical(a->right,b->left);

}

else return false;

}

```
public boolean isSymmetric (BTNode current) {
if (current == null) {
return false;
}
if (current.left() != null && current.right() != null) {
return isSymmetric(current.left()) && isSymmetric(current.right());
} else {
if (current.left() == null && current.right() == null) {
return true;
}
}
return false;
}
```

Do a left->root->right in-order traversal(t1), and a right->root->left in-order traversal(t2), and a left->right->root postorder traversal (t3) and a right->left->root postorder traversal(t4)

the tree is symmetric iff the results of t1==t2 && t3==t4

It's not the most efficient way, but it's easy to implement

- krish July 09, 2013