提交时间:2024-04-16 16:59:02

运行 ID: 143579

#include<bits/stdc++.h> using namespace std; int tree[100005],n; 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(){ 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; }