1064. Complete Binary Search Tree (30)

简介: #include #include #include using namespace std;const int maxn = 1001;vector num(maxn), cbt(maxn);int n, c...
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

const int maxn = 1001;
vector<int> num(maxn), cbt(maxn);
int n, cnt = 0;

void inorder(int root){
    if (root > n) return;
    inorder(root * 2);//左子树
    cbt[root] = num[cnt++];
    inorder(root * 2 + 1);//右子树
}

int main() {
    cin >> n;

    for (int i = 0; i < n; i++) {
        cin >> num[i];
    }
    sort(num.begin(), num.begin() + n);
    inorder(1);

    for (int i = 1; i <= n; i++) {
        printf("%d%c", cbt[i], i == n ? '\n' : ' ');
    }

    return 0;
}
目录
相关文章
|
5月前
|
算法 数据库 索引
Binary Search
二分查找(Binary Search)是一种在有序数组中查找目标值的算法。它的基本思想是将数组分成两半,判断目标值是否在左半部分或右半部分,然后递归地在相应的半部分中查找。这个过程不断重复,直到找到目标值或者确定目标值不存在为止。二分查找的时间复杂度为 O(logn),其中 n 是数组的长度。
37 0
|
算法 Python
Leetcode-Medium 98. Validate Binary Search Tree
Leetcode-Medium 98. Validate Binary Search Tree
108 0
PAT (Advanced Level) Practice - 1043 Is It a Binary Search Tree(25 分)
PAT (Advanced Level) Practice - 1043 Is It a Binary Search Tree(25 分)
103 0
【1064】Complete Binary Search Tree (30 分)
【1064】Complete Binary Search Tree (30 分) 【1064】Complete Binary Search Tree (30 分)
80 0
【1099】Build A Binary Search Tree (30 分)
【1099】Build A Binary Search Tree (30 分) 【1099】Build A Binary Search Tree (30 分)
98 0
【1043】Is It a Binary Search Tree (25 分)
【1043】Is It a Binary Search Tree (25 分) 【1043】Is It a Binary Search Tree (25 分)
103 0
1110. Complete Binary Tree (25)
#include #include #include #include using namespace std; struct node { int ad, l, r; }; vector visited(20, ...
956 0
|
算法 索引 C++
|
索引
leetcode Binary Search
leetcode 35 Search Insert Position Question Given a sorted array and a target value, return the index if the target is found.
1053 0