You are given an array of n integers. You need to insert these elements into 2 binary search trees(BSTs), such that maximum(height(BST-1), height(BST-2)) could be minimized. Elements can be inserted into BST in any order. (Constraints — 1<=n<=10^5)
№ | Пользователь | Рейтинг |
---|---|---|
1 | tourist | 3757 |
2 | jiangly | 3647 |
3 | Benq | 3581 |
4 | orzdevinwang | 3570 |
5 | Geothermal | 3569 |
5 | cnnfls_csy | 3569 |
7 | Radewoosh | 3509 |
8 | ecnerwala | 3486 |
9 | jqdai0815 | 3474 |
10 | gyh20 | 3447 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | maomao90 | 171 |
2 | adamant | 164 |
3 | awoo | 163 |
4 | TheScrasse | 159 |
5 | nor | 155 |
6 | maroonrk | 154 |
7 | -is-this-fft- | 152 |
8 | Petr | 147 |
9 | orz | 145 |
9 | pajenegod | 145 |
You are given an array of n integers. You need to insert these elements into 2 binary search trees(BSTs), such that maximum(height(BST-1), height(BST-2)) could be minimized. Elements can be inserted into BST in any order. (Constraints — 1<=n<=10^5)
Название |
---|
Building BST like that:
For interval [l, r] make element at (l + r) / 2 the root and apply the same process recursively for left child with interval [l, (l + r) / 2 - 1] and right child with interval [(l + r) / 2 + 1, r]
If you're not required to build them and all you need is minimized maximum height, note that to minimize the height we need to make the tree a complete binary tree, and a complete binary will have floor(log2(n)) levels assuming that a tree with a single node has height zero
If you use C++, you can calculate floor(log2(n)) with just:
But what about this condition:maximum(height(BST-1), height(BST-2)) could be minimized??
If I understood him correctly, maximum(height(BST-1), height(BST-2)) will be equal to log2(n / 2).