提交时间:2023-08-23 16:15:14

运行 ID: 99716

#include <bits/stdc++.h> using namespace std; int tree[100005]; int inp[100005]; void buildTree(int cur, int a, int b) { int mid = (a + b) / 2; tree[cur] = inp[mid]; if (mid != a) buildTree(cur * 2, a, mid-1); if (mid != b) buildTree(cur * 2 + 1, mid+1, b); } void dfs(int cur) { if (tree[cur] == 0) return; cout << tree[cur] << " "; cout << "Node " << cur << " L: " << cur * 2 << " R: " << cur * 2 + 1 << endl; dfs(cur * 2); dfs(cur * 2 + 1); } int main() { int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> inp[i]; } sort(inp + 1, inp + n + 1); buildTree(1, 1, n); dfs(1); return 0; }