SWUST OJ 1105: 交换二叉树的孩子结点 C++实现

 题目描述

编程程序实现将二叉树中所有结点的左右孩子互换。

输入

二叉树的先序序列(输入先序序列建立二叉树)。

输出

第一行为交换后的二叉树的中序序列
第二行为交换后的二叉树的先序序列
#include<bits/stdc++.h>
using namespace std;
struct{
	int l, r;
}z[105];
int creat(){
	char ch;
	cin>>ch;
	if(ch=='#') return ch;
	z[ch].l = creat();
	z[ch].r = creat();
	return ch;
}
void swapt(char ch){
	if(ch=='#') return;
	swap(z[ch].l, z[ch].r);
	swapt(z[ch].l);
	swapt(z[ch].r);
}
void dfsm(char c){
	if(c=='#') return;
	dfsm(z[c].l);
	cout<<c;
	dfsm(z[c].r);
}
void dfsf(char c){
	if(c=='#') return;
	cout<<c;
	dfsf(z[c].l);
	dfsf(z[c].r);
}
int main(){
	creat();
    swapt('A');
    dfsm('A');
    cout<<endl;
    dfsf('A');
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Ljy_Cxy/article/details/131472115