Complete Binary Tree | Practice | GeeksforGeeks

Given a Binary Tree, write a function to check whether the given Binary Tree is Complete Binary Tree or not. A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible.

Example 1:
Input:
       1
      / \
     2   3
Output:
Complete Binary Tree
Example 2:
Input:
              1
            /   \
          2      3
           \    /  \
            4  5    6
Output:
Not Complete Binary Tree

Constraints:
1<=Number of Node<=100
0 <= Data of a node <= 106
Your Task:
You don't need to take input. Just complete the function isCompleteBT() that takes root node as a parameter and returns true, if the tree is Complete else returns false.
 
 
0
amishasahu3283 weeks ago
 bool isCompleteBT(Node* root){
        //code here
        if(!root) return true;
        // Intution : 
        // In a complete binary tree you will never encounter null node between the not null nodes.
        queue<Node*> q;
        q.push(root);
        bool foundNull = false;
        while(!q.empty())
        {
            Node *curr = q.front();
            q.pop();
            if(curr->left)
            {
                if(foundNull)
                    return false;
                q.push(curr->left);
            }
            else
                foundNull = true;
            if(curr->right)
            {
                if(foundNull)
                    return false;
                q.push(curr->right);
            }
            else
                foundNull = true;
        }
        return true;
        
    }
0
feyza2 months ago
JAVA

class GfG
{
	boolean isCompleteBT(Node root)
    {
        int index = 0;
        int numNodes = countNodes(root);
        
        if(checkComplete(root, index, numNodes) == true)
            return true;
        
        return false;
	}
	
	boolean checkComplete(Node root, int index, int numberNodes){
	    if(root == null) return true;
	    
	    if(index >= numberNodes)
	        return false;
	        
	    return (checkComplete(root.left, 2 * index + 1, numberNodes)
            && checkComplete(root.right, 2 * index + 2, numberNodes));         
	}
	
	int countNodes(Node root){
	    if(root == null)
	        return 0;
	   
	   return (1+countNodes(root.left) + countNodes(root.right));
	}

}
+1
nonefrompict2 months ago
C++ Code! 
 
bool isCompleteBT(Node* root){       //code here       bool end=false;       queue<Node*>q;       q.push(root);       while(q.empty()==false){           Node* temp=q.front();           q.pop();           if(temp==NULL){               end=true;           }else{               if(end){                   return false;               }               q.push(temp->left);               q.push(temp->right);           }       }       return true;   }
+1
vt28522412 months ago
Easiest Java - 0.1 time
hint - if not null found after null, return false;
 
boolean isCompleteBT(Node root)   {       Queue<Node> q=new LinkedList<>();       q.add(root);       boolean nullFound=false;       while(!q.isEmpty()){           int size=q.size();           for(int i=0; i<size; i++){               Node temp=q.remove();               if(temp==null){                   nullFound=true;               }               else{                   if(nullFound)                   return false;                   q.add(temp.left);                   q.add(temp.right);               }           }       }              return true;} 
0
lindan1232 months ago
bool isCompleteBT(Node* root){
            
            queue<Node*> q;
            q.push(root);
            int flag=0;
            while(q.empty()==false)
            {
                int n = q.size();
                while(n--)
                {
                    Node* temp = q.front();
                    q.pop();
                    
                    if(temp->left!=NULL)
                    {
                        if(flag==1)
                        {
                            return false;
                        }
                        q.push(temp->left);
                    }
                    if(temp->left==NULL)
                    {
                        flag=1;
                    }
                    if(temp->right!=NULL)
                    {
                        if(flag==1)
                        {
                            return false;
                        }
                        else if(flag==0)
                        {
                            q.push(temp->right);
                        }
                    }
                    if(temp->right==NULL)
                    {
                        flag=1;
                    }
                }
            }
            return true;
    }
 
Time Taken : 0.0
Cpp
+2
shashikantsolanki0423 months ago
HINT : In a complete binary tree you will never encounter null between the values. 
+1
badgujarsachin833 months ago
 bool isCompleteBT(Node* root){
        //code here
        queue<Node*> q;
        q.push(root);
        bool found=false;
        while(!q.empty()){
            Node* x=q.front();
            q.pop();
            if(x->left!=NULL){
                if(found){
                    return false;
                }
                q.push(x->left);
            }else{
                found=true;
            }
            if(x->right!=NULL){
                if(found){
                    return false;
                }
                q.push(x->right);
            }else{
                found=true;
            }
        }
        return true;
    }
-1
chessnoobdj4 months ago
C++ 2 approaches
bool isCompleteBT(Node* root){
    if(!root) return true;
    queue<node*> q;
    bool isComplete = false;
    q.push(root);
    while(!q.empty()){
        if(q.front()->left){
            if(isComplete) return false;
            else q.push(q.front()->left);
        }
        else isComplete = true;
        if(q.front()->right){
            if(isComplete) return false;
            else q.push(q.front()->right);
        }
        else isComplete = true;
        q.pop();
    }
    return true;
}
bool isCompleteBT(Node* root){
        string str = "";
        bool flg = false;
        int val = 1;
        queue <Node*> q;
        q.push(root);
        while(!q.empty()){
            int n = q.size();
            if(flg && n != val)
                return false;
            if(val != n)
                flg = true;
            if(flg){
                for(int i=0; str[i]!='\0'; i++){
                    while(str[i] == 'N' && str[i+1] == 'N')
                        i++;
                    if(str[i] == 'N' && str[i+1]!='\0')
                        return false;
                }
            }
            //cout << n << " " << flg << " " << val << " " << str << "\n";
            str = "";
            while(n--){
                Node *temp = q.front();
                q.pop();
                if(temp->left){
                    q.push(temp->left);
                    str += to_string(temp->left->data);
                }
                else
                    str += 'N';
                if(temp->right){
                    q.push(temp->right);
                    str += to_string(temp->right->data);
                }
                else
                    str += 'N';
            }
            val *= 2;
        }
        return true;
    }

-1
sharad nailwal4 months ago
    bool isCompleteBT(Node* root){       queue<Node*> q;       q.push(root);       bool found=false;       while(!q.empty()){           Node* temp=q.front();q.pop();           if(temp->left!=NULL){               if(found)return false;               q.push(temp->left);           }           else found=true;                      if(temp->right!=NULL){               if(found)return false;               q.push(temp->right);           }           else found=true;       }       return true;   }
-1
kake13375 months ago
    bool isCompleteBT(Node* root){       queue<Node*> q;       q.push(root);       bool CT=true;       bool flag=false;       while(!q.empty())       {         int c=q.size();         while(c>0)         {          Node* curr=q.front();               q.pop();           c--;               Node *L=curr->left;               Node *R=curr->right;               if(L!=NULL && R!=NULL)               {                   if(flag)                   {                       CT=false;                       break;                   }                   else                   {                       q.push(L);                       q.push(R);                   }               }               else if(L!=NULL && R==NULL)               {                   if(flag)                   {                       CT=false;                       break;                   }                   else                   {                       flag=true;                       q.push(L);                   }               }               else if(L==NULL && R!=NULL)               {                   CT=false;                   break;               }               else               flag=true;           }           if(!CT)           break;       }       return CT;   }
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.