题解
2025-07-08 21:51:47
发布于:北京
5阅读
0回复
0点赞
#include<bits/stdc++.h>
using namespace std;
struct node{
int value,left,right;
}a[101];
void w(int root){
queue<int> q;
q.push(root);
while(!q.empty()){
int now=q.front();
q.pop();
cout<<a[now].value<<" ";
if(a[now].left!=0){
q.push(a[now].left);
}
if(a[now].right!=0){
q.push(a[now].right);
}
}
}
int main(){
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i].value;
}
for(int i=2;i<=n;i++){
int k=1;
while(1){
if(a[i].value<a[k].value){
if(a[k].left0){
a[k].left=i;
break;
}else{
k=a[k].left;
}
}else{
if(a[k].right0){
a[k].right=i;
break;
}else{
k=a[k].right;
}
}
}
}
w(1);
return 0;
}
这里空空如也
有帮助,赞一个