## Amazon Interview Question for Software Engineer / Developers

Country: United States

Comment hidden because of low score. Click to expand.
14
of 16 vote

correction to the above:
bool ISSimilarBST(Node *a, Node *b)
{
if (a == NULL && b == NULL) return true;
if (a == NULL || b == NULL) return false;
return (a->data == b->data &&
ISSimilarBST(a->left, b->left) &&
ISSimilarBST(a->right, b->right));
}

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

issimilar(node *a,node *b)
{
if((a==NULL||B==null)&&(a==NULL&&b==NULL)
return false
else
return true;
issimilar(a->left,b->left);
issimilar(a->right,b->right);
}

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

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

bool ISSimilar(node *a, node *b)
{
if(a==NULL && b==NULL) return true;
else if(a==NULL || b==NULL) return false;
else return (ISSimilar(a->left, b->left) && ISSimilar(a->right, b->right));
}

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

In the last else part check for the data equality condition also

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

Actually ... The question says, only to check if two trees are similar.......So we should only check the structure of the tree.
If you'll check for the values also, that means they are copy of each other, which is not required.

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

According to the interviewer, the definition of "similarity" is to reflect the data as well as the structure.

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

``````public static boolean isSimilar(Node firstRoot, Node secondRoot) {

if ( firstRoot != null && secondRoot != null ) {
if ( firstRoot.getVal() == secondRoot.getVal() ) {
return isSimilar(firstRoot.getLeft(), secondRoot.getLeft())
&&isSimilar(firstRoot.getRight(), secondRoot.getRight());
} else {
return false;
}
}
return true;
}``````

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

bool compare_bintree(struct bintree *y,struct bintree *i)
{
if(y==NULL&&i==NULL)
{
//cout<<"NULL binary tree";
return true;
}
else
{
if(y!=NULL&&i!=NULL)
{
if(y->l==i->l)
{
return true&&compare_bintree(y->left,i->left)&&compare_bintree(y->right,i->right);
}
else
{
return false;
}

}
else
{
cout<<"not equal";
return false;
}

}
}

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

public static Boolean check_similarity(Tree t1,Tree t2){
if(t1==null && t2==null)
return true;
if(t1==null || t2==null)
return false;
return (check_similarity(t1.left,t2.left))&&(check_similarity(t1.right,t2.right));
}

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.