Is Binary Heap Tree Problem Statement
You are given a binary tree of integers. Your task is to determine if it is a binary heap tree or not.
Input:
The first line of input contains an integer ‘T’ denoting the number of test cases.
The next ‘T’ lines represent the ‘T’ test cases. The only line of input contains the elements of the tree in the level order form separated by a single space.
If any node does not have a left or right child, take -1 in its place.
Output:
For every test case, print a single line containing either "True" if the binary tree is a heap, else print "False".
Example:
Input:
1
1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1
Output:
False
Explanation:
In the given example, the binary tree depicted by the input is not a binary heap tree as it fails to satisfy the complete binary tree property.
Constraints:
1 <= ‘T’ <= 100
1 <= ‘N’ <= 3000
0 <= ‘DATA’ <= 10^9
Note:
A binary heap tree has the following properties:
- It must be a complete binary tree where every level, except the last level, is completely filled and the last level is as far left as possible.
- Every parent node must be greater than all its children nodes, forming a max-heap.
Be the first one to answer
Add answer anonymously...
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

