bool Isomorphism(BiTree T1,BiTree T2);
#include
#include
#define N 100;
using namespace std;
typedef struct BiNode{
char data;
struct BiNode *lchild,*rchild;
}BiTNode,*BiTree;
void CreateBiTree(BiTree &T,char a[],int &i){
char ch;
ch=a[i++];
if(ch=='#') T=NULL;
else{
T=new BiTNode;
T->data=ch;
CreateBiTree(T->lchild,a,i);
CreateBiTree(T->rchild,a,i);
}
}
bool Isomorphism(BiTree T1,BiTree T2);
int main(){
BiTree T1,T2;
int i=0,j=0;
char a[N];
char b[N];
cin>>a>>b;
CreateBiTree(T1,a,i);
CreateBiTree(T2,b,j);
cout<<Isomorphism(T1,T2);
return 0;
}
/* 请在这里填写答案 */
bool Isomorphism(BiTree T1, BiTree T2)
{
if (T1 == NULL && T2 == NULL)
return true;
if (T1 != NULL && T2== NULL || T1 == NULL && T2 != NULL)
return false;
else {
Isomorphism(T1->lchild, T2->lchild);
Isomorphism(T1->rchild, T2->rchild);
}
}
本题构造一个含3个结点的二叉树,输入的第一个结点为根结点,第二个结点为根结点的左儿子,第三个结点为根结点的右儿子,输出这个二叉树的先序、中序和后序序列。
bool Isomorphism(BiTree T1,BiTree T2);
#include
#include
#define N 100
using namespace std;
typedef struct BiNode{
char data;
struct BiNode *lchild,*rchild;
}BiTNode,*BiTree;
void CreateBiTree(BiTree &T,char a[],int &i){
char ch;
ch=a[i++];
if(ch=='#') T=NULL;
else{
T=new BiTNode;
T->data=ch;
CreateBiTree(T->lchild,a,i);
CreateBiTree(T->rchild,a,i);
}
}
bool Isomorphism(BiTree T1,BiTree T2);
int main(){
BiTree T1,T2;
int i=0,j=0;
char a[N];
char b[N];
cin>>a>>b;
CreateBiTree(T1,a,i);
CreateBiTree(T2,b,j);
cout<<Isomorphism(T1,T2);
return 0;
}
/* 请在这里填写答案 */
Bptr creat()
{
Bptr B;
B = (Bptr)malloc(sizeof(Bnode));
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
B->data = a;
B->Lson = (Bptr)malloc(sizeof(Bnode));
B->Rson = (Bptr)malloc(sizeof(Bnode));
B->Lson->data = b;
B->Rson->data = c;
return B;
}
void preorder(Bptr p)
{
printf("%d ", p->data);
printf("%d ", p->Lson->data);
printf("%d ", p->Rson->data);
}
void inorder(Bptr p)
{
printf("%d ", p->Lson->data);
printf("%d ", p->data);
printf("%d ", p->Rson->data);
}
void postorder(Bptr p)
{
printf("%d ", p->Lson->data);
printf("%d ", p->Rson->data);
printf("%d ", p->data);
}
求根结点到x结点的路径(假定结点值不重复)。
bool Findxpath(BiTree bt,char x,vector
#include
#include
#include
using namespace std;
typedef struct BiNode{
char data;
struct BiNode *lchild,*rchild;
}BiTNode,*BiTree;
void CreateBiTree(BiTree &T){
char ch;
cin>>ch;
if(ch=='#') T=NULL;
else{
T=new BiTNode;
T->data=ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
}
bool Findxpath(BiTree bt,char x,vector<char> tmppath,vector<char> &path);
int main(){
BiTree T;
char x;
vector<char>tmppath;
vector<char>path;
CreateBiTree(T);
cin>>x;
Findxpath(T,x,tmppath,path);
for(int i=0;i<path.size();i++)
cout<<path[i]<<" ";
return 0;
}
/* 请在这里填写答案 */
bool flag = false;
bool Findxpath(BiTree bt, char x, vector<char> tmppath, vector<char>& path)
{
if (bt != NULL)
{
tmppath.push_back(bt->data);
if (bt->data != x)
{
if (bt->lchild != NULL)
Findxpath(bt->lchild, x, tmppath, path);
if (bt->rchild != NULL && !flag)
Findxpath(bt->rchild, x, tmppath, path);
}
else
{
flag = true;
path = tmppath;
return true;
}
}
if (flag)
return true;
else
return false;
}