1 solutions

  • 0
    @ 2024-12-10 22:44:37

    C++ :

    #include <bits/stdc++.h>
    using namespace std;
    
    int n, x;
    int a[1009], cnt;
    
    void dfs(int u) {
    	if (!a[u]) return;
    	
    	dfs(u * 2);
    	cout << a[u] << ' ';
    	dfs(u * 2 + 1);
    }
    
    int main() {
    	cin >> n;
    	while (n --) {
    		memset(a, 0, sizeof(a));
    		cnt = 0;
    		
    		while (cin >> x && x != -1) {
    			a[++ cnt] = x;
    		}
    		
    		cout << int(log2(cnt)) + 1 << ' ';
    		dfs(1);
    		cout << endl;
    	}
    
    	return 0;
    }
    
    • 1

    Information

    ID
    556
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    (None)
    Tags
    (None)
    # Submissions
    0
    Accepted
    0
    Uploaded By