2977 二叉堆练习1

2977 二叉堆练习1

 

时间限制: 10 s
空间限制: 32000 KB
题目等级 : 白银 Silver
 
 
 
 
题目描述 Description

已知一个二叉树,判断它是否为二叉堆(小根堆)

输入描述 Input Description

二叉树的节点数N和N个节点(按层输入)

输出描述 Output Description

YES或NO

样例输入 Sample Input

样例输入1

3

1 4 9

样例输入2

3

6 4 9

样例输出 Sample Output

样例输出1

YES

样例输出2

NO

数据范围及提示 Data Size & Hint

对于20%的数据  N≤20

对于50%的数据  N≤1000

对于100%的数据 N≤50000,每个节点≤10000

分类标签 Tags

 1 #include<iostream>
 2 using namespace std;
 3 long long int a[100001];
 4 int main()
 5 {
 6     int n;
 7     cin>>n;
 8     for(int i=1;i<=n;i++)
 9     {
10         cin>>a[i];
11     }
12     int flag=0;
13     for(int i=1;i<=n;i++)
14     {
15         if((a[i*2]<a[i]&&a[i*2]!=0)||(a[i*2+1]<a[i]&&a[i*2+1]!=0))
16         {
17             flag=1;
18             break;
19         }
20     }
21     if(flag==1)
22     {
23         cout<<"NO";
24     }
25     else
26     {
27         cout<<"YES";
28     }
29     return 0;
30  } 

 

posted @ 2017-03-31 21:20  自为风月马前卒  阅读(203)  评论(0编辑  收藏  举报

Contact with me