4.6 Design an algorithm and write code to find the first common ancestor of two nodes in a binary tree. Avoid storing additional nodes in a data structure. NOTE: This is not necessarily a binary search tree.


BT

Node find(Node n, Node a, Node b)
{
  if (n == a || n == b)
  {
    return n; 
  }
  
  int nodeMatch = nodeMatch(a.left);
  if (nodeMatch == 0)
    return find(n.right, a, b);
  else if (nodeMatch == 1)
    return n;
  else
    return find(r.left, a, b);
}


int nodeMatch(Node n, Node a, Node b)
{
  if (n == null)
    return 0;
  
  int toReturn = 0;
  if (n == a || n == b)
    toReturn ++;
  toReturn += find(n.left) + find(n.right);
}