判断树B是否为树A的子树&&求出树的镜像

判断树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;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值