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;
}
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