Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

How are most students funded?

Answered: 1 week ago