Commonly asked Data Structures and Algorithms Problems by big tech and different solution approaches with code in Java and C

Powered by Blogger.

Tuesday, August 8, 2017

Is Binary Tree MaxHeap


Idea is to check values recursively as MAX heap property.


Code:

 boolean flag=true;
    void heap(Node tree,int val)
    {
        if(tree.left!=null)
       { if(tree.left.data>val){
            flag= false;
        }
           heap(tree.left,tree.data);
       }
       if(tree.right!=null)
       { if(tree.right.data>val){
            flag= false;
        }
           heap(tree.right,tree.data);
       }
       
    }
    boolean isHeap(Node tree)
    {
  if(tree==null)
  return true;
  heap(tree,tree.data);
  return flag;
    }

0 Comments:

Post a Comment

Stats