


A binary tree is a tree in which each parent node has at most two children.
A binary heap tree has the following properties.
1. It must be a complete binary tree. In the complete binary tree every level, except the last level, is completely filled and the last level is as far left as possible.
2. Every parent must be greater than its all children nodes.
Consider this binary tree:

Figure 1 is a complete binary tree because every level, except the last level, is completely filled and the last nodes are as far left as possible, and the level has 2 nodes both on the left side.
Figure 2 is not a complete binary tree because level 2 (level is 0 based) is not completely filled means the right child of the node (36) is missing.
There is another reason, in the last level, there can be one another node in between node (1) and node (14) to make the binary tree as far left as possible.
1. In the world of programming two types of binary heap tree possible:
a. Min-heap - if all parents nodes are lesser than its children nodes.
b. Max-heap - if all parents nodes greater than its children nodes, explained in the above figure-1.
2. In this problem binary heap tree is a binary max-heap tree.
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. Refer to the example below.
Elements are in the level order form. The input consists of values of nodes separated by a single space in a single line. In case a node is null, we take -1 in its place.
For example, the input for the tree depicted in the below image would be :

1
2 3
4 -1 5 6
-1 7 -1 -1 -1 -1
-1 -1
Explanation :
Level 1 :
The root node of the tree is 1
Level 2 :
Left child of 1 = 2
Right child of 1 = 3
Level 3 :
Left child of 2 = 4
Right child of 2 = null (-1)
Left child of 3 = 5
Right child of 3 = 6
Level 4 :
Left child of 4 = null (-1)
Right child of 4 = 7
Left child of 5 = null (-1)
Right child of 5 = null (-1)
Left child of 6 = null (-1)
Right child of 6 = null (-1)
Level 5 :
Left child of 7 = null (-1)
Right child of 7 = null (-1)
The first not-null node (of the previous level) is treated as the parent of the first two nodes of the current level. The second not-null node (of the previous level) is treated as the parent node for the next two nodes of the current level and so on.
The input ends when all nodes at the last level are null (-1).
The above format was just to provide clarity on how the input is formed for a given tree.
The sequence will be put together in a single line separated by a single space. Hence, for the above-depicted tree, the input will be given as:
1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1
For every test case, print a single containing either "True" if the binary tree is a heap else print "False".
You do not need to print anything; it has already been taken care of. Just implement the given function.
1 <= 'T' <= 100
1 <= 'N' <= 3000
0 <= 'DATA' <= 10 ^ 9
Where ‘N’ is the number of nodes in the tree, and ‘DATA’ denotes data contained in the node of a binary tree.
Time Limit: 1 sec.
The idea is that we keep a check of both the properties of a binary heap one by one. Firstly, we check if the given tree is a complete binary tree and secondly, we check if all the parent nodes are greater than their child nodes. The above figure (3,8) represents that the level of the node with data (1) is 3 and the index is 8.
Consider this binary tree:
The idea is that we place all nodes(position) as shown in the above figure.
Suppose we are at node (position) then the left child of this node will be (position * 2) and the right child of this node will be (position * 2 + 1).
In the end, we check if the last node position is equal to the number of nodes in the given tree. It will ensure that the given binary tree is a complete binary tree. For binary heap trees, we check every node is greater than children.