面试题18:树的子结构

/*************************************************************************
    > Created Time: Fri 22 Apr 2016 10:40:35 AM PKT
 ************************************************************************/

#include<iostream>
using namespace std;

struct BinaryTreeNode{
    int value;
    BinaryTreeNode* left;
    BinaryTreeNode* right;
};

typedef BinaryTreeNode b;



/*
//is a==b
bool IsEqual(BinaryTreeNode* a,BinaryTreeNode* b)
{
    if(a==NULL){
        if(b==NULL){
            return true;
        }else{
            return false;
        }
    }else{
        if(b==NULL){
            return false;
        }else{
            if(a->value!=b->value){
                return false;
            }else{
                return IsEqual(a->left,b->left) && IsEqual(a->right,b->right);
            }
        }
    }

    return false;
}


//is b a a's sub tree?
bool IsSubTree(BinaryTreeNode* rootA,BinaryTreeNode* rootB)
{
    bool result=false;
    bool bleft=false;
    bool bright=false;

    //first, find b in a
//  if(rootA==NULL){
//      return false;
//  }
if(rootA!=NULL){
    if(rootA->value==rootB->value){
        //is a==b?
        result=IsEqual(rootA,rootB);
        if(result==false){
            bleft=IsSubTree(rootA->left,rootB);
            bright=IsSubTree(rootA->right,rootB);
            result=bleft || bright;
        }

    }else{
        bleft=IsSubTree(rootA->left,rootB);
        bright=IsSubTree(rootA->right,rootB);
        result=bleft || bright;
    }
}
    return result;
}

*/


BinaryTreeNode* creat(int value,BinaryTreeNode* left=NULL,BinaryTreeNode* right=NULL){
    BinaryTreeNode* node=new BinaryTreeNode;
    node->value=value;
    node->left=left;
    node->right=right;

    return node;
}

void PrintTree(b* root)
{
    if(root==NULL){
        return ;
    }
    cout<<root->value<<ends;
    PrintTree(root->left);
    PrintTree(root->right);
}




//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//arow to offer
//树的子结构
bool DoesTree1HaveTree2(b* t1,b* t2)
{
    if(t2==NULL){
        return true;
    }
    if(t1==NULL){
        return false;
    }
    if(t1->value!=t2->value){
        return false;
    }

    return DoesTree1HaveTree2(t1->left,t2->left) && DoesTree1HaveTree2(t1->right,t2->right);
}

bool HasSubtree(b* rootA,b* rootB)
{
    bool result=false;
    if(rootA!=NULL && rootB!=NULL){
        if(rootA->value==rootB->value){
            result=DoesTree1HaveTree2(rootA,rootB);
        }
        if(result==false){
            result=HasSubtree(rootA->left,rootB);
        }
        if(result==false){
            result=HasSubtree(rootA->right,rootB);
        }
    }
    return result;
}

int main(int argc,char** argv)
{

//  b* d=creat(9);
//  b* e=creat(2);
//  b* b=creat(8,d,e);
    b* af=creat(4);
    b* ag=creat(7);
    b* ae=creat(2,af,ag);
    b* ad=creat(9);
    b* ab=creat(8,ad,ae);
    b* ac=creat(7);
    b* a=creat(8,ab,ac);
    cout<<"tree a is: "<<ends;
    PrintTree(a);
    cout<<endl;

    b* d=creat(9);
    b* e=creat(2);
    b* b=creat(8,d,e);
    cout<<"tree b is: "<<ends;
    PrintTree(b);
    cout<<endl;

    if(HasSubtree(a,b)){
        cout<<"b is one of a's subtree"<<endl;
    }else{
        cout<<"b is not one of a's sub tree"<<endl;
    }

    return 0;

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值