2977 binary heap exercises

Source: Internet
Author: User

2977 binary heap exercises
2977 binary heap Exercise 1

 

Time Limit: 10 s space limit: 32000 KB title level: Silver Title Description Description

Determine whether a binary tree is a binary heap (small root heap)

Input description Input Description

Number of nodes in a binary tree N and N (input by layer)

Output description Output Description

YES or NO

Sample Input Sample Input

Example input 1

3

1 4 9

Sample input 2

3

6 4 9

Sample output Sample Output

Sample output 1

YES

Sample output 2

NO

Data range and prompt Data Size & Hint

For 20% of data, N ≤ 20

For 50% of data, N ≤ 1000

For 100% of data, N ≤ 50000, each node ≤ 10000

CATEGORY tag Tags click here to expand
 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     else26     {27         cout<<"YES";28     }29     return 0;30  } 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.