判断树B是否为树A的子树。
思路很简单,以B的根节点为标准,查找A中节点是否有相同的。如果有相同的,再判断两个子树是否相同。
如果不同,则继续查找。
#include<iostream>
#include<string>
#include<queue>
using namespace std;
struct Binarytree{
int value;
Binarytree *left;
Binarytree *right;
};
Binarytree* Buildtree(){
int x;
scanf("%d",&x);
if(x == 0)
return NULL;
queue<Binarytree *> Bq;
Binarytree* root = (Binarytree *)malloc(sizeof(Binarytree));
root->value = x;
root->left = NULL;
root->right = NULL;
Binarytree* temp = root;
Bq.push(temp);
while(!Bq.empty()){
temp = Bq.front();
Bq.pop();
if(temp->left == NULL){
int x;
printf("输入%d的左结点\n",temp->value);
scanf("%d",&x);
if(x!=0){
Binarytree * tleft = (Binarytree *)malloc(sizeof(Binarytree));
tleft->value = x;
tleft->left = tleft->right = NULL;
temp->left = tleft;
Bq.push(tleft);
}
}
if(temp->right == NULL){
int x;
printf("输入%d的右结点\n",temp->value);
scanf("%d",&x);
if(x!=0){
Binarytree * tright = (Binarytree *)malloc(sizeof(Binarytree));
tright->value = x;
tright->left = tright->right = NULL;
temp->right = tright;
Bq.push(tright);
}
}
}
return root;
}
//判断A和B是否相同
bool IsTreeBinTreeA(Binarytree* pA,Binarytree* pB){
if(pB == NULL)
return true;
if(pA == NULL)
return false;
if(pA->value != pB->value)
return false;
return IsTreeBinTreeA(pA->left,pB->left)&&IsTreeBinTreeA(pA->right,pB->right);
}
//B的根节点在A中查找
bool IsSubTree(Binarytree* pA,Binarytree* pB){
bool IsSub = false;
if(pA->value == pB->value)
IsSub = IsTreeBinTreeA(pA,pB);
if(!IsSub)
IsSub = IsSubTree(pA->left,pB);
if(!IsSub)
IsSub = IsSubTree(pA->right,pB);
return IsSub;
}
int main(){
printf("Create Binary tree A:\n");
Binarytree* pA = Buildtree();
printf("Create Binary tree B:\n");
Binarytree* pB = Buildtree();
bool result = IsSubTree(pA,pB);
if(result)
printf("B is in A");
else
printf("B is not in A");
system("PAUSE");
return 0;
}
求树的镜像。即递归的交换左子树和右子树即可,在递归过程中左右子树都为空则直接返回。
这两道题目都很好的利用了树的递归性质。
#include<iostream>
#include<string>
#include<queue>
using namespace std;
struct Binarytree{
int value;
Binarytree *left;
Binarytree *right;
};
Binarytree* Buildtree(){
int x;
scanf("%d",&x);
if(x == 0)
return NULL;
queue<Binarytree *> Bq;
Binarytree* root = (Binarytree *)malloc(sizeof(Binarytree));
root->value = x;
root->left = NULL;
root->right = NULL;
Binarytree* temp = root;
Bq.push(temp);
while(!Bq.empty()){
temp = Bq.front();
Bq.pop();
if(temp->left == NULL){
int x;
printf("输入%d的左结点\n",temp->value);
scanf("%d",&x);
if(x!=0){
Binarytree * tleft = (Binarytree *)malloc(sizeof(Binarytree));
tleft->value = x;
tleft->left = tleft->right = NULL;
temp->left = tleft;
Bq.push(tleft);
}
}
if(temp->right == NULL){
int x;
printf("输入%d的右结点\n",temp->value);
scanf("%d",&x);
if(x!=0){
Binarytree * tright = (Binarytree *)malloc(sizeof(Binarytree));
tright->value = x;
tright->left = tright->right = NULL;
temp->right = tright;
Bq.push(tright);
}
}
}
return root;
}
void Getmirror(Binarytree* root){
if(root == NULL || (root->left == NULL&&root->right == NULL))
return;
Binarytree* temp = root->left;
root->left = root->right;
root->right= temp;
if(root->left)
Getmirror(root->left);
if(root->right)
Getmirror(root->right);
}
void printTree(Binarytree* root){
if(root == NULL)
return;
queue<Binarytree *> Bq;
Bq.push(root);
while(!Bq.empty()){
root = Bq.front();
Bq.pop();
printf("%d",root->value);
if(root->left)
Bq.push(root->left);
if(root->right)
Bq.push(root->right);
}
printf("\n");
}
int main(){
Binarytree* root= Buildtree();
Getmirror(root);
printTree(root);
system("PAUSE");
return 0;
}