天天看點

zcmu--4932: 樹查找

4932: 樹查找

Time Limit: 1 Sec  Memory Limit: 32 MB

Submit: 7  Solved: 5

[Submit][Status][Web Board]

Description

有一棵樹,輸出某一深度的所有節點,有則輸出這些節點,無則輸出EMPTY。該樹是完全二叉樹。

Input

輸入有多組資料。

每組輸入一個n(1<=n<=1000),然後将樹中的這n個節點依次輸入,再輸入一個d代表深度。

Output

輸出該樹中第d層得所有節點,節點間用空格隔開,最後一個節點後沒有空格。

Sample Input

5

1 2 3 4 5

7

7

1 2 3 4 5 6 7

2

Sample Output

EMPTY

2 3

HINT

Source

資料結構高分筆記

【分析】下标查找就好  水題

#include<bits/stdc++.h>
using namespace std;
int a[1005];
int main()
{
	int n;
	while(~scanf("%d",&n)&&n)
	{
		for(int i=0;i<n;i++)
			scanf("%d",&a[i]);
		int d;
		scanf("%d",&d);
		if(pow(2,d-1)>a[n-1])
		{
			cout<<"EMPTY\n";
			continue;
		}
		else{
			int x=pow(2,d-1);
			int y=min(n,x+x-1);
			int flag=0;
			for(int i=x-1;i<y;i++)
			{
				if(!flag)cout<<a[i],flag=1;
				else cout<<" "<<a[i];
			}
			cout<<endl;
		}
	}
	return 0;
}