//转置二叉树仅需节点的左右子树互换
层次遍历+中序遍历
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
const int N =10+5;
struct Node{
int l, r;
Node(){
l = r = -1;
}
}T[N];
bool vis[N];
bool flag = true;
void inorder(int root) {
if (root == -1) return;
inorder(T[root].l);
if (flag) flag = false;
else cout << " ";
cout << root ;
inorder(T[root].r);
}
void levelorder(int root) {
queue<int> q;
q.push(root);
while (!q.empty()) {
int tmp = q.front();
q.pop();
if (flag) flag = false;
else cout <<" ";
cout << tmp;
if (T[tmp].l != -1) q.push(T[tmp].l);
if (T[tmp].r != -1) q.push(T[tmp].r);
}
}
int main() {
int n;
char a, b;
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> a >> b;
//直接交换节点左右子树即可
if(a != '-') T[i].r = a-'0';
if(b != '-') T[i].l = b-'0';
vis[a-'0'] = true;
vis[b-'0'] = true;
}
int root;
for (int i = 0; i < n; ++i) {
if (vis[i] == 0) root = i;
}
levelorder(root);
cout << endl;
flag = true;
inorder(root);
return 0;
}