开发者

Compare Two Binary Trees

开发者 https://www.devze.com 2023-02-18 07:20 出处:网络
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 bread

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));
}
0

精彩评论

暂无评论...
验证码 换一张
取 消