Advertisement
Guest User

Untitled

a guest
Mar 30th, 2015
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.89 KB | None | 0 0
  1. string AlienFamilyTree::lowest_common_ancestor(NodePtr root, string name1,
  2.                                             string name2)
  3. {
  4.     if(root == NULL)
  5.     {
  6.         return "";
  7.     }
  8.  
  9.     if(is_descendant(root->data, name1) && is_descendant(root->data, name2))
  10.     {
  11.         string a = lowest_common_ancestor(root->left, name1, name2),
  12.                b = lowest_common_ancestor(root->right, name1, name2);
  13.     if(a == "" && b=="") { // This is the lowest common ancestor
  14.              return root->data;
  15.         } else if (a != "") { // The left subtree contained the lowest common ancestor
  16.              return a;
  17.         } else if (b != "" { // The right subtree contained the lowest common ancestor
  18.              return b;
  19.         } else { // WHAT THE HELL????????????????????????????? } // I have no idea
  20.     } else { // This subtree does not contain both children
  21.     return "";
  22.     }
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement