PAT A1043

镜像树的创建即在插入insert函数中将原本向左右子树递归的条件交换一下即可

void insert1(node* &root,int v)
{
	if(root==NULL)//没找到,说明要插入
	{
		root=new_node(v);
		return;
	}
	if(v>=root->data) insert1(root->rchild,v);
	else insert1(root->lchild,v);
}
void insert2(node* &root,int v)//生成镜像树
{
	if(root==NULL)//没找到,说明要插入
	{
		root=new_node(v);
		return;
	}
	if(v<root->data) insert2(root->rchild,v);//只需要反过来即可
	else insert2(root->lchild,v);
}

完整代码如下

#include<bits/stdc++.h>
using std::cin;
using std::cout;
using std::endl;
using std::string;
struct node
{
	int data;
	node *lchild,*rchild;
};
int pre[1005],post[1005];
node* new_node(int v)//新建一个权值为v的结点
{
	node *new_node=new node;
	new_node->data=v;
	new_node->lchild=new_node->rchild=0;
	return new_node;
}
void insert1(node* &root,int v)
{
	if(root==NULL)//没找到,说明要插入
	{
		root=new_node(v);
		return;
	}
	if(v>=root->data) insert1(root->rchild,v);
	else insert1(root->lchild,v);
}
void insert2(node* &root,int v)//生成镜像树
{
	if(root==NULL)//没找到,说明要插入
	{
		root=new_node(v);
		return;
	}
	if(v<root->data) insert2(root->rchild,v);//只需要反过来即可
	else insert2(root->lchild,v);
}
node* create1(int a[],int n)//建树
{
	node *root=NULL;
	for(int i=0;i<n;i++) 
	{
		insert1(root,a[i]);
	}
	return root;
}
node* create2(int a[],int n)//建镜像树
{
	node *root=NULL;
	for(int i=0;i<n;i++) 
	{
		insert2(root,a[i]);
	}
	return root;
}
node* find_min(node *root)//以root为根结点查找子树中最小的结点,查找root的后继应该使用find_min(root->rchild);
{
	while(root->lchild!=NULL)
	{
		root=root->lchild;
	}
	return root;
}
node* find_max(node *root)//以root为根结点查找子树中最大的结点,查找root的前驱应该使用find_max(root->lchild);
{
	while(root->rchild!=NULL)
	{
		root=root->rchild;
	}
	return root;
}
int sum1,sum2;
void pre_order(node *root)
{
	if(root==NULL) return;
//	printf("%d ",root->data);
	pre[sum1++]=root->data;
	pre_order(root->lchild);
	pre_order(root->rchild);
}
void post_order(node *root)
{
	if(root==NULL) return;
	post_order(root->lchild);
	post_order(root->rchild);
	post[sum2++]=root->data;
}
void windows_cmd_support_utf8(void)
{
	#ifdef WIN32
	    system("chcp 65001 & cls");
	#endif
}
int n;
bool compare(int a[],int b[])
{
	for(int i=0;i<n;i++)
	{
		if(a[i]!=b[i]) return false;
	}
	return true;
}
signed main(void)
{
	windows_cmd_support_utf8();
	int a[1005];
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
	}
	node *root1=new node;
	root1=create1(a,n);
	pre_order(root1);
	if(compare(a,pre)==true)
	{
		printf("YES\n");
		post_order(root1);
		for(int i=0;i<n-1;i++)
		printf("%d ",post[i]);
		printf("%d",post[n-1]);
		return 0;
	}
	else if(compare(a,pre)==false)
	{
		sum1=0;
		sum2=0;
		node *root2=new node;
		root2=create2(a,n);
		pre_order(root2);
		if(compare(a,pre)==true)
		{
			printf("YES\n");
			post_order(root2);
			for(int i=0;i<n-1;i++)
			printf("%d ",post[i]);
			printf("%d",post[n-1]);
			return 0;
		}
		else
		{
			printf("NO\n");
			return 0;
		}
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据引用中的说明,给定一个字符串"pat"和一个长度为104的字符串,我们需要将104个字符按照PATest的顺序重新排列,并忽略其他字符。根据引用中的代码,我们可以使用一个map来统计各个字符的出现次数。然后,我们按照PATest的顺序循环输出字符直到所有字符都被输出,每输出一个字符就将其对应的出现次数减少1。 具体步骤如下: 1. 创建一个map来统计字符出现次数。 2. 循环遍历104个字符,如果字符是"P"、"A"、"T"、"e"、"s"、"t"中的任意一个,就将其出现次数加1。 3. 创建一个字符串s1="PATest",用于按照PATest的顺序输出字符。 4. 进入一个循环,循环条件为flag等于1,表示还有字符需要输出。 5. 在循环中,遍历字符串s1,如果字符在map中的出现次数大于0,则将该字符输出,并将其出现次数减1。 6. 如果在循环中有字符被输出,则将flag设为1,表示还有字符需要输出。 7. 当循环结束后,所有字符都被输出,输出结果为PATest。 因此,根据给定的字符串"pat"和长度为104的字符串,输出应为PATest。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [PAT 1043 输出PATest](https://blog.csdn.net/qq_61117990/article/details/123307930)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [PAT 1043 输出PATest (c++)](https://blog.csdn.net/qq_42334822/article/details/122637564)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值