文章

16

粉丝

134

获赞

0

访问

3.4k

头像
二叉搜索树 - 复旦2020 题解:Time Limit Exceeded 90% 有什么办法可以缩短时间呢
P996 复旦大学2020年机试题
发布于2024年4月8日 09:36
阅读数 182

#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] <...
登录查看完整内容


登录后发布评论

3 条评论
snake
2024年4月8日 10:01

可以试一下加速输入输出,改用scanf

赞(0)

为欢几何 : 回复 snake: 这个改了还是不太行【头秃】

2024年4月8日 15:28

snake : 回复 为欢几何: 那就是算法需要改进了,可以看看这个优化思路:https://noobdream.com/post/370949/

2024年4月9日 12:20