Is Binary Heap Tree
You have been given a binary tree of integers.
Your task is to check if it is a binary heap tree or not.
Note:
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.
For example:
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.
Note:
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.
Input Format:
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.
Example:
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).
Note :
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
Output Format:
For every test case, print a single containing either "True" if the binary tree is a heap else print "False".
Note:
You do not need to print anything; it has already been taken care of. Just implement the given function.
Constraint :
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.
CodingNinjas
author
2y
Firstly, I solved this using DFS and BFS approach that takes O(N) time so the interviewer asked me to optimize it. Then I used this property of 2*parent,2*parent+1 to solve it in O(logN), I constructe...read more
CodingNinjas
author
2y
Recursion
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 no...read more
CodingNinjas
author
2y
Breadth-first search
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 (po...read more
Add answer anonymously...
Top DE Shaw Associate Developer Intern interview questions & answers
Top HR questions asked in DE Shaw Associate Developer Intern
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app