Is Binary Tree Heap | Practice | GeeksforGeeks

Given a binary tree. The task is to check whether the given tree follows the max heap property or not.
Note: Properties of a tree to be a max heap - Completeness and Value of node greater than or equal to its child.
Example 1:
Input:
      5
    /  \
   2    3
Output: 1
Explanation: The given tree follows max-heap property since 5,
is root and it is greater than both its children.


Example 2:
Input:
       10
     /   \
    20   30 
  /   \
 40   60
Output: 0

Your Task:
You don't need to read input or print anything. Your task is to complete the function isHeap() which takes the root of Binary Tree as parameter returns True if the given binary tree is a heap else returns False.

Expected Time Complexity: O(N)
Expected Space Complexity: O(N)
Constraints:
1 ≤ Number of nodes ≤ 100
1 ≤ Data of a node ≤ 1000
0
aggarwaldeepali4542 days ago
Solution for checking both conditions : 
Tree is complete or not
Is it heap or not
I have used Level order traversal 😊
 bool isHeap(struct Node* tree) {              if(tree == NULL)       return true;              queue<Node *> q;       q.push(tree);        bool f = false;        while(!q.empty()){              Node* node = q.front();           q.pop();                   if(node->left)           {               if(f == true)                 return false;                                else if(node->left->data > node->data)                 return false;                              else                 q.push(node->left);                             }                      else           {               if(f == false)                 f = true;           }                      if(node->right){               if(f == true)                   return false;                                  else if(node->right->data > node->data)                   return false;                             else                   q.push(node->right);             }                      else{                 if(f == false)                   f = true;             }        }           return true;   }             
0
vishalgoelcoder2 days ago
Overall good question, can come in Medium Category.
0
rahul soni 13 weeks ago
 pair<bool,int> check(struct Node* root,int val){   if(root==NULL)   return {true,0};   if(root->data>val)   return {false,0};   pair<bool,int>a=check(root->left,root->data);   pair<bool,int>b=check(root->right,root->data);   if(!(a.first && b.first)){       return {false,0};   }   else if(a.second < b.second)       return {false,0};   else return {true,a.second+1};    }
bool isHeap(struct Node* root) {   return check(root,INT_MAX).first;}
0
koulikmaity4 weeks ago
bool isCBT(struct Node* root, int index, int nodeCount)   {       // base case       if(root == NULL )           return true;              if(index >= nodeCount)           return false;       else       {           bool left = isCBT(root->left, 2*index + 1, nodeCount);           bool right = isCBT(root->right, 2*index + 2, nodeCount);           return (left && right);        }          }   bool isMaxOrder(struct Node* root)   {       if(root->left == NULL && root->right == NULL)           return true;       if(root->right == NULL)           return (root->data > root->left->data);       else       {           return ( (root->data > root->left->data) && (root->data > root->right->data) && isMaxOrder(root->left) && isMaxOrder(root->right) );       }          }   int nodeCount(struct Node* root)   {       if(root == NULL)           return 0;              return 1 + nodeCount(root->left) + nodeCount(root->right);   }      bool isHeap(struct Node* root) {              int index = 0;       int totalCount = nodeCount(root);       if(isCBT(root, index, totalCount ) && isMaxOrder(root))           return true;       else           return false;   }
0
rampurnaveen91 month ago
bool check_complete_binarytree(Node *root)   {       struct Node *temp = root;       int c=0;       while(temp!= NULL)       {           temp = temp->left;           c++;       }       temp = root;       int d=0;       while(temp != NULL)       {           temp = temp->right;           d++;       }       return (c>=d);   }   bool max_heap_check(Node *root,bool &temp)   {       if(root->left == NULL && root->right == NULL)           return true;       else if(root->left != NULL && root->right == NULL)       {           if(root->data > root->left->data)           {               max_heap_check(root->left,temp);           }           else               temp = false;       }       else if(root->right != NULL && root->left == NULL)       {           if(root->data > root->right->data)           {               max_heap_check(root->right,temp);           }           else               temp = false;       }       else if(root->left != NULL && root->right != NULL)       {           if(root->data > root->left->data && root->data > root->right->data)           {               max_heap_check(root->left,temp);               max_heap_check(root->right,temp);           }           else           {               temp = false;           }       }   }   bool isHeap(struct Node* root)    {       bool temp = true;       if(check_complete_binarytree(root))           max_heap_check(root,temp);       else           temp = false;       return temp;       // code here   }
+2
hharshit81181 month ago
class Solution { public:   bool isHeap(struct Node* root) {       // code here       queue<Node*> q;       q.push(root);       while(!q.empty()){           Node* x = q.front();           if(x == NULL){               break;           }           q.pop();           if(x->left){               if(x->left->data > x->data)               return false;               else               q.push(x->left);           }           else{               q.push(NULL);           }           if(x->right){               if(x->right->data > x->data)               return false;               else               q.push(x->right);           }           else{               q.push(NULL);           }       }       while(q.empty() == false){           if(q.front() != NULL)           return false;           q.pop();       }       return true;   }};
I/P : 8 7 6 N 5 3 2 
q(8)
q(7, 6)
q(6, N, 5)
q(N, 5, 3, 2)
q.front() == NULL, break;
q.front() ≠ NULL;
returned false here.
O/P : 0
+1
brij bhusan pandey1 month ago
why below input is not maxHeap,
8 7 6 N N 5 4
0
ritesh274403
This comment was deleted.
0
milindprajapatmst192 months ago
class Solution {
  public:
    int countNodes(Node* root) {
        if (!root)
            return 0;
        return 1 + countNodes(root->left) + countNodes(root->right);
    }
    bool isComplete(Node* root, int k, int cnt) {
        if (!root)
            return true;
        if (k > cnt)
            return false;
        return isComplete(root->left, k << 1, cnt) && isComplete(root->right, k << 1 | 1, cnt);
    }
    bool checkValue(Node* root) {
        if (!root)
            return true;
        if (root->left && root->data < root->left->data)
            return false;
        if (root->right && root->data < root->right->data)
            return false;
        return checkValue(root->left) && checkValue(root->right);
    }
    bool isHeap(Node* root) {
        int cnt = countNodes(root);
        return isComplete(root, 1, cnt) && checkValue(root);
    }
};
+1
sanketbhagat2 months ago
SIMPLE JAVA SOLUTION
class Solution {
    boolean isHeap(Node root) {
        // code here
        if(root==null || (root.left==null && root.right==null)) return true;
        if(root.left==null && root.right!=null) return false;
        if(root.right!=null && root.right.left!=null && root.left.right==null) return false;
        if(root.left.data>root.data) return false;
        if(root.right!=null && root.right.data>root.data) return false;
        return isHeap(root.left) && isHeap(root.right);
    }
}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.