## Microsoft Interview Question

SDE-3s**Country:**United States

**Interview Type:**In-Person

Farah-Colton Bender algorithm is for the generic case where given a tree (a K-tree, not necessarily Binary), we have to make "repeated" queries about the LCA of any two arbitrary nodes.

Hence a preprocessing step is kind of mandatory (just like in Tries or Suffix Trees)

For this question, can we not solve it by finding the "point of convergence" of two linked list ? In this case it is a point of divergence, rather !

Provided an iterative solution with O(1) space complexity.

Assumed the tree is mutable.

The idea is to do the in-order traversal with O(1) space complexity

using the Morris traversal technique.

Note that the LCA node is the node at the minimum depth

among nodes visited after finding the first one.

```
BTNode* findLCAIterative(BTNode* node, int data1, int data2)
{
bool found = false;
BTNode *ret = nullptr;
int minDepth = numeric_limits<int>::max();
int depth = 0;
while(node){
if(node->left){
// check whether we visited all of left-side decendents
BTNode *tmp = node->left;
int tmpDepth = 0;
while(tmp->right && tmp->right != node){
tmp = tmp->right;
tmpDepth++;
}
if(!tmp->right){
// we haven't visted any of left-side decendents
// create back link for the return
// and visit left child
tmp->right = node;
node = node->left;
depth++;
continue;
}
// we have visited all of left decendents
// recover the pointer
tmp->right = nullptr;
// update the depth
// considering one link for left, another for the back link
depth -= tmpDepth + 2;
// we recovered all back links
if(found && depth == 0)
break;
// set LCA to current node if needed
if(!found && ret && depth < minDepth) {
ret = node;
minDepth = depth;
}
}
// visit current
if(!found && (node->data == data1 || node->data == data2)){
if(ret) {
// we found the LCA
// but we need to finish this traversal
// to recover temporary links
found = true;
}
else {
// found the first one
ret = node;
minDepth = depth;
}
}
node = node->right;
depth++;
}
return found ? ret : nullptr;
}
```

Simple recursive tree iteration algorithm requires O(n), where n is a number of nodes in the tree.

Iterative algorithm requires O(n) for calculating parents of the nodes (if no pointers to parent inside node structure are available) and O(h) running time, where h is tree height. If tree is balanced h = log N, hence O(log N).

One of the best solutions is Farah - Colton and Bender algorithm. It requires O(n) for preprocessing and O(1) for each online request. But it quiet complex.

We can also modify iterative algorithm so that it takes O(n log n) for preprocessing and O(log n) for lookup calculating each 2^k, k = 0, 1, ... parent for each node and using this information to speed up a lookup.

Recursive implementation (simple, but not effective):

```
struct node {
struct node *left;
struct node *right;
char key; // For simplicity
};
struct node* lca(struct node *head, struct node *n1, struct node *n2) {
if (!head || !n1 || !n2) { return nullptr; }
if (head->key == n1->key || head->key == n2->key) { return head; }
struct node *p1 = lca(head->left, n1, n2);
struct node *p2 = lca(head->right, n1, n2);
if (p1 && p2) { return head; }
return p1 ? p1 : p2;
}
```

- akhil.gupta@imaginea.com January 27, 2016