XMU 数据结构实验 交换二叉树中每个结点的左孩子和右孩子

以二叉链表作为二叉树的存储结构,交换二叉树中每个结点的左孩子和右孩子。

输入格式:

输入二叉树的先序序列。

提示:一棵二叉树的先序序列是一个字符串,若字符是‘#’,表示该二叉树是空树,否则该字符是相应结点的数据元素。

输出格式:

输出有两行:

第一行是原二叉树的中序遍历序列;

第二行是交换后的二叉树的中序遍历序列。

输入样例:

ABC##DE#G##F###

输出样例:

CBEGDFA

AFDGEBC

是期中考试题喵~

虽然不难但是我在create的时候卡了一会,有点懵逼,可能没绕过弯来……还是太菜了

直接贴代码,一点零五了,去睡觉

#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<queue>
#include<cstring>
using namespace std;
typedef struct bitree{
	char data;
	struct bitree *lc;
	struct bitree *rc;
}bitree;
bitree *create()
{
	bitree *t=(bitree*)malloc(sizeof(bitree));
	char c;
	scanf("%c",&c);
	if(c!='#'){
		t->data=c;
		t->lc=create();
		t->rc=create();
	}
	else return NULL;
	return t;
}
void swap_t(bitree *t)
{
	bitree *left,*right;
	left=t->lc;
	right=t->rc;
	t->lc=right;
	t->rc=left;
	if(t->lc)swap_t(t->lc);
	if(t->rc)swap_t(t->rc);
}
void mid_print(bitree *t)
{
	if(t->lc)mid_print(t->lc);
	cout<<t->data;
	if(t->rc)mid_print(t->rc);
}
int main(){
	bitree *T;
	T=create();
	mid_print(T);
	cout<<endl;
	swap_t(T);
	mid_print(T);
	return 0;
} 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值