Given two binary search trees, write function whic开发者_StackOverflowh tells if two such trees are the same – (i.e. same info in the nodes, same branching to left and right at every node).
Do a breadth- or depth-first search on both trees and check each node for equality as you iterate through.
Try this:
bool bst::isequal1(node *& root1,node *& root2)
{
if(root1==NULL && root2==NULL)
{ return true;}
if( (root1==NULL && root2!=NULL) || (root1!=NULL && root2==NULL) )
{
return false;
}
if( (root1->data) != (root2->data) )
{
return false;
}
return ( isequal1(root1->left,root2->left) &&
isequal1(root1->right,root2->right));
}
精彩评论