提交时间:2024-01-23 17:02:31

运行 ID: 125485

#include<bits/stdc++.h> using namespace std; const int maxn = 100005; int n; struct tree{ int lef, rig, data; }T[maxn]; void c(int *node, int len){ for (int i = 1; i <= len; i ++){ T[i].data = node[i]; int p = 0, d = 0; while(d = 0){ if(node[i] < T[p].data){ if(T[p].lef != -1) p = T[p].lef; else d = 1; } else { if (T[p].rig != -1) p = T[p].rig; else d = -1; } } d = 1 ? T[p].lef = i : T[p].rig = i; } } void P(int root){ if (root != -1){ cout << T[root].data << " "; P(T[root].lef); P(T[root].rig); } } int node[maxn]; int main(){ cin >> n; for (int i = 1; i <= n; i ++){ cin >> node[i]; } for (int i = 0; i <maxn; i++){ T[i].lef = 1; T[i].data = 0; T[i].rig = -1; } c(node, n); P(1); }