#include<iostream>
using namespace std;
struct node {
char value;
node * lchild;
node * rchild;
};
node * create()
{
node * pnode=NULL;
char t;
cin>>t;
if(t=='#') return NULL;
else
{
pnode = (node*)malloc(sizeof(node));
pnode->value=t;
pnode->lchild=create();
pnode->rchild=create();
}
return pnode;
}
bool HaveTree(node * proot,node * root2);
bool HasSubTree(node * proot,node * root2)
{
bool flag = false;
//if(root2==NULL) return true;
if(proot!=NULL&&root2!=NULL)
{
if(proot->value==root2->value)
{
flag= HaveTree(proot,root2);
}
if(!flag)
flag=HasSubTree(proot->lchild,root2);
if(!flag)
flag=HasSubTree(proot->rchild,root2);
}
return flag;
}
bool HaveTree(node * proot,node * root2)
{
if(root2==NULL) return true;
if(proot==NULL) return false;
if(proot->value!=root2->value) return false;
return HaveTree(proot->lchild,root2->lchild)&&HaveTree(proot->rchild,root2->rchild);
}
void lrd(node * proot)
{
if(proot!=NULL)
{
lrd(proot->lchild);
lrd(proot->rchild);
cout<<proot->value<<" ";
}
}
int main()
{
node * root =create();
lrd(root);
cout<<endl;
node * root2 =create();
lrd(root2);
cout<<endl;
cout<<HasSubTree(root,root2)<<endl;
return 0;
}
剑指offer查找二叉树中的子树结构
最新推荐文章于 2019-07-13 12:11:12 发布