Write a function to determine whether 2 binary search trees are similar (same sh
ID: 3576145 • Letter: W
Question
Write a function to determine whether 2 binary search trees are similar (same shape but not necessarily the same data). The header should be template <typename T> bool are—similar (const binary_tree_node<T>* nodel, const binary_tree_node<T>* node2); Write a function to determine whether 2 binary search trees are similar (same shape but not necessarily the same data). The header should be template <typename T> bool are—similar (const binary_tree_node<T>* nodel, const binary_tree_node<T>* node2); Write a function to determine whether 2 binary search trees are similar (same shape but not necessarily the same data). The header should be template <typename T> bool are—similar (const binary_tree_node<T>* nodel, const binary_tree_node<T>* node2);Explanation / Answer
template <typename T>
bool are—similar (const binary_tree_node<T>* node1, const binary_tree_node<T>* node2)
{
//Case 1:If both are empty then they are same
if(node1==NULL && node2==NULL)
return true;
//Case 2:If 2 non empty then compare them
if(node1!=NULL &&node2!=NULL)
{
return
{
node1->data==node2->data && are—similar(node1->left,node2->left)
&& are—similar(node1->right,node2->right)
};
}
//Case 3:One empty,one not false
return 0;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.