Amazon Interview Question
Software Engineer in TestsCountry: United States
struct BTNode LCA(struct BTNode *root, struct BTNode *a, struct BTNode *b)
{
struct BTNode *left, *right;
if(root == NULL)
return root;
if(root==a || root ==b)
return root;
left= LCA(root -> left, a,b);
right=LCA(root-> right,a,b);
if(left && right)
return root;
else
return(left?left:right)
}
Node *CommonAncestor(Node *Root, Node *N1, Node *N2){
Node *LC = NULL, *RC = NULL;
if(Root == NULL || N1 == NULL || N2 == NULL) return NULL;
if(Root == N1 || Root == N2) return Root;
LC = CommonAncestor(Root->Left, N1, N2);
RC = CommonAncestor(Root->Riht, N1, N2);
if(LC != NULL && RC != NULL) return Root;
else if(LC != NULL) return LC;
else if(RC != NULL) return RC;
else return NULL;
}
refer question?id=14991835
- siva December 08, 2012