1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
#include <iostream> #include <cmath>
using namespace std;
const int N = 1e5 + 10;
int a[N];
int find(int n, int dept) { for (int i = 2; i <= n; i++) { if (pow(2, dept + 1) - 1 == i) dept++; }
return dept; }
void findTree(int n, int dept) { if (a[dept]) cout << a[dept] << " "; if (a[dept * 2] && 2 * dept <= n) findTree(n, 2 * dept); if (a[dept * 2 + 1] && 2 * dept + 1 <= n) findTree(n, 2 * dept + 1); }
int main() { int n; cin >> n;
a[1] = 1; for (int i = 2; i <= n; i++) a[i] = i;
findTree(n, 1); find(n, 1); }
|