L2-035 完全二叉树的层序遍历

#include <bits/stdc++.h>

using namespace std;

const int N = 50;
int tr[N]; 
int n;

void dfs(int cnt) { 
    if (cnt > n) return;
    dfs(cnt * 2);
    dfs(cnt * 2 + 1);
    cin >> tr[cnt];
}

int main() {
    cin >> n;
    dfs(1);
    for (int i = 1; i <= n; i++) {
        cout << tr[i] << " "[i == n];
    }

    return 0;
}
posted @ 2022-04-16 14:54  Xxaj5  阅读(23)  评论(0编辑  收藏  举报