Amazon Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: Phone Interview
just find the inorder and preorder of both tree and compare both the array ..if same tree is same otherwize not..
1) If both trees are empty return false
2) else if both trees are non-empty
a. check data of root nodes
b. check left sub tree recursively
c. check right sub tree recursively
3 Else return false
int sametree(struct node *t1, struct node *t2)
{
if(t1->data == NULL) && (t2->data==NULL))
return 0;
else if(t1->data!=NULL && t2->data!=NULL)
{
return (t1->data == t2->data) &&
sametree( t1->left,t2->left) &&
sametree(t1->right,t2->right);
}
else
return 0;
}
- Shivaprasad April 12, 2012