二叉树的子结构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
struct BinaryTreeNode
{
    int value;
    BinaryTreeNode *left,*right;
};
bool doestree1havetree2(BinaryTreeNode *root1,BinaryTreeNode *root2);
bool hassubtree(BinaryTreeNode *root1,BinaryTreeNode *root2)
{
      //notice that result is static
      static bool result=false;
    if(root1!=NULL&&root2!=NULL)
    {
        if(root1->value==root2->value)
        {
            result=doestree1havetree2(root1,root2);
        }
        if(!result)hassubtree(root1->left,root2);
        if(!result)hassubtree(root1->right,root2);
    }
    return result;
}
bool doestree1havetree2(BinaryTreeNode *root1,BinaryTreeNode *root2)
{
    if(root2==NULL)
        return true;
    if(root1==NULL)
        return false;
    if(root1->value!=root2->value)
        return false;
    return doestree1havetree2(root1->left,root2->left)&&doestree1havetree2(root1->right,root2->right);
}

转载:http://www.cnblogs.com/tgkx1054/archive/2013/01/09/2853589.html

posted @ 2013-05-23 18:53  simon1024  阅读(112)  评论(0编辑  收藏  举报