面试题18:树的子结构

来源:互联网 发布:淘宝女生船袜 编辑:程序博客网 时间:2024/04/28 20:55
/*************************************************************************    > 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==bbool 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;}
0 0