SWUST OJ 1105: バイナリ ツリーの子ノードを交換する C++ 実装

 トピックの説明

プログラミング プログラムは、バイナリ ツリー内のすべてのノードの左右の子の交換を実現します。

入力

バイナリ ツリーのプレオーダー シーケンス (バイナリ ツリーを構築するためのプレオーダー シーケンスを入力します)。

出力

1 行目は交換された 2 分木の inorder シーケンス
2 行目は交換された 2 分木の preorder シーケンス
#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