Water problem, just review the binary tree, and then explore the law of the binary tree
Article Directory
Question stem
4932: Tree lookup
Time Limit: 1 Sec Memory Limit: 32 MB
Submit: 46 Solved: 28
[Submit][Status][Web Board]
Description
There is a tree that outputs all nodes of a certain depth, if there is a tree, these nodes are output, and if there is no tree, EMPTY is output. The tree is a complete binary tree.
Input
There are multiple sets of data entered.
Enter one n for each group (1<=n<=1000), then enter the n nodes in the tree in turn, and enter a d for depth.
Output
Output all nodes in the d-th level of the tree, separated by spaces, and there is no space after the last node.
Sample Input
5
1 2 3 4 5
7
7
1 2 3 4 5 6 7
2
0
Sample Output
EMPTY
2 3
idea
This question is actually the law of the
binary tree. The law of the binary tree is that the first branch of the nth layer is pow(2, n-1), and the nth layer has pow(2, n-1) nodes, then the idea of this question is very simple , Open directly
AC code
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e3+10;
typedef long long ll;
int a[maxn];
int main()
{
int n,d;
while(cin>>n&&n)
{
memset(a, 0, sizeof a);
for(int i=1;i<=n;i++)cin >> a[i];
cin >> d;
int start = pow(2,d-1);
if(start>n)puts("EMPTY");
else{
int len = 2*start - 1;
//题目说了这是一个完全二叉树,所以下一行可以省去
if(len>n)len = n;
for(int i=start;i<=len;i++){
if(i!=start)printf(" ");
printf("%d",a[i]);
}
puts("");
}
}
return 0;
}
to sum up
Water problem