代码:
#include <bits/stdc++.h>
using namespace std;
const int maxn=1e5 + 10;
struct Node{
int ls,rs;
}a[maxn];
void dfs(int u){
if(u=='*')
return;
cout << u;
dfs(a[u].ls);
dfs(a[u].rs);
}
int main(){
int n,r;
cin >> n;
for(int i=1;i<=n;i++){
int x,y,z;
cin >> x >> y >> z;
if(i==1) {
r=x;
}
a[x].ls = y;
a[x].rs = z;
}
dfs(r);
}
题目:
给你一个大小为n的树,输出此树的中序遍历
第一行一个整数n表示大小为n的树
接下来一共n行每行有两个整数a,b 第 i行表示 点i的左子节点是a,右子节点是b,倘若a或b为 −1则说明没有此左儿子或者右儿子
输出一行
4
2 3
4 -1
-1 -1
-1 -1
4213
n<=105