文章
16
粉丝
134
获赞
0
访问
11.1k
#include<bits/stdc++.h>
using namespace std;
int temp[100001];
struct Tree {
int data;
struct Tree* lchild;
struct Tree* rchild;
};
//二叉搜索树插入
void _insert(struct Tree* &root, int t) {
if(root == NULL) {
root = new Tree;
root->data = t;
root->lchild = NULL;
root->rchild = NULL;
return;
} else if(t < root->data) {
temp[t] = root->data;
_insert(root->lchild, t);
} else if(t > root->data) {
temp[t] = root->data;
_insert(root->rchild, t);
} else if(t == root->data)
return;
}
int main() {
int n;
cin >> n;
int num[n];
struct Tree *root = NULL;
for(int i = 1; i <= n; i++) {
cin >> num[i];
}
temp[num[1]] = 0;
for(int i = 1; i <= n; i++) {
_insert(root, num[i]);
}
for(int i = 1; i <= n; i++) {
cout << temp[i] <...
登录后发布评论
可以试一下加速输入输出,改用scanf