提交时间:2023-08-23 16:37:38

运行 ID: 99735

#include <bits/stdc++.h> using namespace std; int tree[100005]; void putInt(int i, int curAt) { if (tree[curAt] == -1) { tree[curAt] = i; return; } if (i < tree[curAt]) putInt(i, curAt * 2); else putInt(i, curAt * 2 + 1); } void dfs(int cur) { if (tree[cur] == -1) return; cout << tree[cur] << " "; dfs(cur * 2); dfs(cur * 2 + 1); } int main() { int n; cin >> n; memset(tree, -1, sizeof tree); for (int i = 1; i <= n; i++) { int x; cin >> x; putInt(x, 1); } dfs(1); return 0; }