Asked inDE Shaw,Associate Developer Intern

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...
DE Shaw Associate Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
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

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter