Question: DLList common _ ancestors ( const T & val 1 , const T & val 2 ) const { if ( ! contains ( val

DLList common_ancestors(const T & val1, const T & val2) const {
if (!contains(val1)||!contains(val2)){
return DLList();
}
DLList ancestors;
if (val1== val2){
BSTNode* node = root;
while (node){
ancestors.add_to_tail(node->val);
if (val1< node->val){
node = node->left;
} else if (val1> node->val){
node = node->right;
} else {
break;
}
}
} else {
findCommonAncestors(root, val1, val2, ancestors);
}
return ancestors;
// ADD YOUR IMPLEMENTATION HERE
} bool BST::findCommonAncestors(BSTNode* node, const T& val1, const T& val2, DLList& ancestors) const {
if (!node)
return false;
bool foundInLeft = findCommonAncestors(node->left, val1, val2, ancestors);
bool foundInRight = findCommonAncestors(node->right, val1, val2, ancestors);
if ((foundInLeft && foundInRight)||(node->val == val1|| node->val == val2)){
ancestors.add_to_tail(node->val);
return true;
}
return foundInLeft || foundInRight ||(node->val == val1|| node->val == val2);
} where is the error in the above code when i get this error Oops something is wrong with your solution:
Nodes were inserted into the BST in this order: 1851667258
Your result from common_ancestors(18,51): {51,18}
Correct result from common_ancestors(18,51): {18}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!