Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- string AlienFamilyTree::lowest_common_ancestor(NodePtr root, string name1,
- string name2)
- {
- if(root == NULL)
- {
- return "";
- }
- if(is_descendant(root->data, name1) && is_descendant(root->data, name2))
- {
- string a = lowest_common_ancestor(root->left, name1, name2),
- b = lowest_common_ancestor(root->right, name1, name2);
- if(a == "" && b=="") { // This is the lowest common ancestor
- return root->data;
- } else if (a != "") { // The left subtree contained the lowest common ancestor
- return a;
- } else if (b != "" { // The right subtree contained the lowest common ancestor
- return b;
- } else { // WHAT THE HELL????????????????????????????? } // I have no idea
- } else { // This subtree does not contain both children
- return "";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement