文章
37
粉丝
1
获赞
314
访问
7.9k
#include<bits/stdc++.h>
using namespace std;
int n;
int a[100010];
int idx;
typedef struct node{
int data;
node* left;
node* right;
}*BiTree;
BiTree build(int minv ,int maxv){
if(idx >= n) return NULL;
if(a[idx] < minv || a[idx] > maxv) return NULL;
BiTree root = new node{a[idx],NULL,NULL};
idx++;
root->left = build(minv,root->data);
root->right = build(root->data,maxv);
return root;
}
void LevelOrder(BiTree root){
queue<node*> q;
q.push(root);
while(!q.empty()){
BiTree curr = q.front();
q.pop();
cout<<curr->data<<' ';
if(curr->left!=nullptr) q.push(curr->left);
if(curr->right!=nullptr) q.push(curr->right);
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin>>n;
BiTree root = nullptr;
for(int i = 0 ; i < n; i++){
cin>>a[i];
}
idx = 0;
root = build(-1e10,1e10);
LevelOrder(root);
}
&...
登录后发布评论
暂无评论,来抢沙发