Is This Binary Tree a Max-Heap?
Updated
Problem statement
Given the root of a binary tree, return true if the tree is a max-heap and false otherwise. A binary tree is a max-heap when both of these hold:
- It is complete: every level is full except possibly the last, and the nodes of the last level are as far left as possible (no gaps).
- It is in heap order: every node's value is greater than or equal to the values of its children.
Function
isMaxHeap(root: TreeNode) → booleanExamples
Example 1
root = [50,30,40,10,20,35]return = trueEvery level is full except the last, which is filled from the left (10, 20, 35). Each parent is at least as large as its children: 50 over 30 and 40, 30 over 10 and 20, 40 over 35. So it is a max-heap.
Example 2
root = [50,30,40,null,20]return = falseThe values are in heap order, but 30 has a right child and no left child. The last level is not filled from the left, so the tree is not complete and not a heap.
Example 3
root = [7,7,9]return = falseThe tree is complete, but the right child 9 is larger than its parent 7, so heap order fails. (Equal values, like the left child 7, are allowed.)
Constraints
- The tree has between
1and2000nodes. -10^4 <= node.val <= 10^4- The tree does not have to be balanced. It can be a single chain of
2000nodes.
More Adobe Phone Screen questions
- Evaluate a Parenthesized Integer ExpressionPHONE SCREEN · Seen Oct 2026
- All Elements of Two BSTs in OrderPHONE SCREEN · Seen Aug 2026
- Rotate an Array In PlaceOA · PHONE SCREEN · Seen Aug 2026
- Binary Tree Level-Order Traversal by LevelsPHONE SCREEN · ONSITE INTERVIEW · Seen Aug 2026
- Count Occurrences in a Sorted ArrayPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2026
- Count Pairs with Absolute Difference KPHONE SCREEN · Seen Jul 2026
- Count Subsets with Sum KPHONE SCREEN · Seen Jul 2026
- Numbers with Exactly Three DivisorsPHONE SCREEN · Seen Jul 2026
- Sum of Even Nodes on a Path in a Numbered Binary TreePHONE SCREEN · Seen Jul 2026
- Largest Value in Every Sliding WindowPHONE SCREEN · Seen Jul 2026
Similar questions at other companies
- Validate a Max HeapRippling · OARippling interview questions
- Cheapest Flights Within K StopsAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Top K Pins with Insert and QueryPinterest · PHONE SCREENPinterest interview questions
- Maximum-Scoring Consecutive Tree LevelsJane Street · PHONE SCREENJane Street interview questions
- Minimum Edits to a Perfect Binary TreeSnowflake · PHONE SCREENSnowflake interview questions
- Find All Paths for a Name in a File SystemThe Walt Disney Company · PHONE SCREENThe Walt Disney Company interview questions
- Worst-Performing Ads by CTR in a Campaign TreeUber · PHONE SCREENUber interview questions
- Check a Complete Binary TreeGoldman Sachs · PHONE SCREENGoldman Sachs interview questions
- Binary Tree Right Side ViewGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Same Binary TreeOdoo · PHONE SCREENOdoo interview questions