## Amazon Interview Question for Consultants

Country: United States
Interview Type: Phone Interview

Comment hidden because of low score. Click to expand.
10
of 12 vote

``````public boolean recurseSymmetry(Node<AnyType> left, Node<AnyType> right ){
if(left == null || right == null) return left==right;
else
return left.value == right.value && recurseSymmetry(left.left, right.right) && recurseSymmetry(left.right, right.left);
}``````

Comment hidden because of low score. Click to expand.
-1
of 1 vote

You're only checking the outer nodes

Comment hidden because of low score. Click to expand.
0

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

Comment hidden because of low score. Click to expand.
0

What is the complexity of this solution?

Comment hidden because of low score. Click to expand.
0

Your code will not work !

Comment hidden because of low score. Click to expand.
0

Inorder traversal output should be a palindrome for a symmetric btree. O(n).

Comment hidden because of low score. Click to expand.
1
of 1 vote

Recursive checking
1. Each node contains 2 leaves, if none then all height must equal

Comment hidden because of low score. Click to expand.
0
of 0 vote

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;
}

Comment hidden because of low score. Click to expand.
0

Your code will not work !!

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````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;
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

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

Comment hidden because of low score. Click to expand.
0

I think it is a applicable way to find the answer! excellent!

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

### Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

### Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.