Perfect Binary Tree | Practice | GeeksforGeeks

Given a Binary Tree, write a function to check whether the given Binary Tree is a prefect Binary Tree or not. A Binary tree is Perfect Binary Tree in which all internal nodes have two children and all leaves are at same level.
Example 1:
Input: 
          7
      /  \
     4    9
Output: YES
Explanation: 
As the root node 7 has two children and 
two leaf nodes 4 and 9 are at same level 
so it is a perfect binary tree.

Example 2:
Input: 
                   7
              /   \
             3     8
           /   \     \
          2     5     10
        /
       1
Output: NO

Your task:
You don't need to read input or print anything. Your task is to complete the function isPerfect() which takes root node of the tree as input parameter and returns a boolean value.If the tree is a perfect binary tree return true other wise return false.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1<=T<=10^5
1<=N<=10^5
1<=data of node<=10^5
0
bansallakshay0812 days ago
int height(Node* root){       if(root==NULL) return 0;       if(root->left==NULL&&root->right==NULL) return 1;       return 1+max(height(root->left),height(root->right));   }   int totalnode(Node* root){       if(root==NULL) return 0;       if(root->left==NULL&&root->right==NULL) return 1;       return 1+totalnode(root->left)+totalnode(root->right);   }   bool isPerfect(Node *root)   {       //code here       int n=height(root);       int nodes=totalnode(root);       // cout<<n<<" "<<nodes<<endl;       if(nodes==pow(2,n)-1) return true;       return false;      }
0
vrajeshmodi992 months ago
int btHeight(Node*root){    
    if(!root)return 0;   
    return 1+max(btHeight(root->left),btHeight(root->right));    
 }
    
    int btNodes(Node*root){
        if(!root)return 0;
        int l= btNodes(root->left);
        int r = btNodes(root->right);
        return l+r+1; 
    }
    bool isPerfect(Node *root)
    {
        int bth = btHeight(root);
        int btn = btNodes(root);
//Note: Perfect-BT no. Nodes=2^H-1;(where H is height of tree)
        if(pow(2,bth)-1 == btn)return true;
        return false;
    }
0
feyza2 months ago
class Solution{
    public boolean isPerfect(Node root){
        int d = depth(root);
        return is_Perfect(root,d,0);     
    }
    
    public boolean is_Perfect(Node root, int d,int l){
        if(root == null) return true;
        
        if(root.left ==null && root.right == null)  return (d==l+1);
        if(root.left ==null || root.right == null)  return false;
        
        return (is_Perfect(root.left, d,l+1) && is_Perfect(root.right,d,l+1));   
        
    }
        
    static int depth(Node node){
        int d = 0;
        while(node != null){
            d++;
            node = node.left;
        }
        return d;
    }
}
0
dattatraygujar773 months ago
  Queue<Node>q=new LinkedList<>();
        q.add(root);
        while(!q.isEmpty())
        {
         int n=q.size();
           for(int i=0;i<n;i++)
           {
             Node temp=q.poll();
            if(temp.left!=null)  q.add(temp.left);
            if(temp.right!=null)   q.add(temp.right);
           }
         if(q.size()!=2*n && q.size()>0)  return false;
        }
         return true;
0
19euee0683 months ago
simple java solution
 
class Solution{
    class Bool{
        boolean isPerfect=true;
    }
    public int getHeight( Node root , 
    					  Bool flag){
        if( flag.isPerfect==false || 
        	root == null ) return 0;
        
        int left=getHeight(root.left,flag);
        int right=getHeight(root.right,flag);
        
        if( left!=right ) 
        	flag.isPerfect = false;
        
        return 1+Math.max( left , right );
    }
    public boolean isPerfect(Node root){
        Bool bool = new Bool();
        getHeight( root , bool );
        return bool.isPerfect;
    }
}
0
yadavkapil23363 months ago
    bool f(struct Node* root, int i,int v)   {       if(root==NULL)       return(true);       if(root->left==NULL && root->right!=NULL||root->left!=NULL && root->right==NULL)       return(false);       if(root->left==NULL && root->right==NULL)       if(v!=i)       return(false);      bool x=f(root->left,i+1,v);       bool y=f(root->right,i+1,v);       if(x&&y)       return(true);       else       return(false);   }
   bool isPerfect(struct Node *root)   {       int v=0;       struct Node* temp=root;       while(temp->left!=NULL)       {           temp=temp->left;           v++;       }       int i=0;                        return(f(root,i,v));          }
0
mayank20214 months ago
C++bool isPerfect(Node *root)   {       int lvar=INT_MIN;              level(root, 1, &lvar);              return (lvar==-1)?0:1;   }      void level(Node *root, int l, int* lvar)   {       if(root && !root->left && !root->right  )       {           if(*lvar==INT_MIN)             *lvar=l;           else if (*lvar !=l )             {                *lvar=-1;                 return;               }                  }       else if(root->left && root->right)        {            level(root->left, l+1, lvar );            level(root->right, l+1, lvar);        }       else         {            *lvar=-1;          return;          }      }
0
sk77887754 months ago
class Solution{
    public boolean isPerfect(Node root){
        //code here
        return (solve(root)==-1)?false:true;
    }
    
    int solve(Node root) {
        if(root.left==null && root.right==null)
            return 1;
        if(root.left==null || root.right==null)
            return -1;
        int c1=solve(root.left);
        int c2=solve(root.right);
        if(c1==-1 || c2==-1 || c1!=c2)
            return -1;
        else
            return 1+Math.max(c1,c2);
    }
}
0
nekhatperveen4 months ago
JAVA
class Solution{   int depth = -1;   public boolean isPerfect(Node root)
   {      int count=1;      boolean is_perfectBinary = getTreeStatus(root, count);      return is_perfectBinary;   }   boolean getTreeStatus(Node root, int count)   {       int c= count;       if(root==null)           return true;       else if(root.left==null && root.right!=null)           return false;       else if(root.left!=null && root.right==null)           return false;       else if(root.left==null && root.right==null)       {           if (depth == -1)           {               depth = c;               return true;           }           else if(c != depth)            {               return false;            }           else               return true;
       }       else if(root.left!=null && root.right!=null)           {               c++;               boolean leftTree = getTreeStatus(root.left, c);               boolean rightTree = getTreeStatus(root.right, c);               return (leftTree && rightTree);
           }       return true;   }}
+1
deepuyadavze4 months ago
// check if minimum height and maximum height are same then return true else return false

class Solution
{
    int mx(Node* root){
        if(root == NULL){
            return 0;
        }
        if(root->left==NULL && root->right==NULL){
            return 1;
        }
        return max(mx(root->left), mx(root->right))+1;
    }
    
    int mn(Node* root){
        if(root==NULL){
            return 0;
        }
        if(root->left==NULL && root->right==NULL){
            return 1;
        }
        return min(mn(root->left), mn(root->right))+1;
    }
public:
    bool isPerfect(Node *root)
    {
        //code here
        // if max height and min height are same than tree is perfect
        int maxHeight=mx(root);
        int minHeight=mn(root);
        
        return maxHeight==minHeight;
    }
};
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.