Check whether a given Binary Tree is Complete or not (Iterative Solution)
Last Updated : 23 Jul, 2025
Given a Binary Tree, the task is to check whether the given Binary Tree is a Complete Binary Tree or not. A complete binary treeis a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible.
Examples:
The following trees are examples of Complete Binary Trees:
The following trees are examples of Non-Complete Binary Trees:
[Expected Approach - 1] Using Level-Order Traversal - O(n) Time and O(n) Space
The idea is to do a level order traversal starting from the root. In the traversal, once a node is found which is Not a Full Node, all the following nodes must be leaf nodes. A node is ‘Full Node’ if both left and right children are not empty (or not NULL).
Also, one more thing needs to be checked to handle the below case: If a node has an empty left child, then the right child must be empty.
Below is the implementation of the above approach:
C++
// C++ implementation to check if a // Binary Tree is complete#include<bits/stdc++.h>usingnamespacestd;classNode{public:intdata;Node*left;Node*right;Node(intx){data=x;left=nullptr;right=nullptr;}};// Function to check if the binary tree is completeboolisCompleteBinaryTree(Node*root){if(root==nullptr)returntrue;queue<Node*>q;q.push(root);boolend=false;while(!q.empty()){Node*current=q.front();q.pop();// Check left childif(current->left){if(end)returnfalse;q.push(current->left);}else{// If left child is missing,// mark the endend=true;}// Check right childif(current->right){if(end)returnfalse;q.push(current->right);}else{// If right child is missing,// mark the endend=true;}}returntrue;}intmain(){// Representation of Input tree// 1// / \ // 2 3// / \ /// 4 5 6Node*root=newNode(1);root->left=newNode(2);root->right=newNode(3);root->left->left=newNode(4);root->left->right=newNode(5);root->right->left=newNode(6);if(isCompleteBinaryTree(root))cout<<"True"<<endl;elsecout<<"False"<<endl;return0;}
Java
// Java implementation to check if a // Binary Tree is completeimportjava.util.LinkedList;importjava.util.Queue;classNode{intdata;Nodeleft;Noderight;Node(intx){data=x;left=null;right=null;}}classGfG{// Function to check if the binary// tree is completestaticbooleanisCompleteBinaryTree(Noderoot){if(root==null)returntrue;Queue<Node>q=newLinkedList<>();q.add(root);booleanend=false;while(!q.isEmpty()){Nodecurrent=q.poll();// Check left childif(current.left!=null){if(end)returnfalse;q.add(current.left);}else{// If left child is missing,// mark the endend=true;}// Check right childif(current.right!=null){if(end)returnfalse;q.add(current.right);}else{// If right child is missing,// mark the endend=true;}}returntrue;}publicstaticvoidmain(String[]args){// Representation of Input tree// 1// / \// 2 3// / \ /// 4 5 6Noderoot=newNode(1);root.left=newNode(2);root.right=newNode(3);root.left.left=newNode(4);root.left.right=newNode(5);root.right.left=newNode(6);if(isCompleteBinaryTree(root))System.out.println("True");elseSystem.out.println("False");}}
Python
# Python implementation to check if a # Binary Tree is completefromcollectionsimportdequeclassNode:def__init__(self,x):self.data=xself.left=Noneself.right=None# Function to check if the binary # tree is completedefis_complete_binary_tree(root):ifrootisNone:returnTrueq=deque([root])end=Falsewhileq:current=q.popleft()# Check left childifcurrent.left:ifend:returnFalseq.append(current.left)else:# If left child is missing,# mark the endend=True# Check right childifcurrent.right:ifend:returnFalseq.append(current.right)else:# If right child is missing, # mark the endend=TruereturnTrueif__name__=="__main__":# Representation of Input tree# 1# / \# 2 3# / \ /# 4 5 6root=Node(1)root.left=Node(2)root.right=Node(3)root.left.left=Node(4)root.left.right=Node(5)root.right.left=Node(6)ifis_complete_binary_tree(root):print("True")else:print("False")
C#
// C# implementation to check if a // Binary Tree is completeusingSystem;usingSystem.Collections.Generic;classNode{publicintdata;publicNodeleft;publicNoderight;publicNode(intx){data=x;left=null;right=null;}}// Function to check if the binary// tree is completeclassGfG{staticboolisCompleteBinaryTree(Noderoot){if(root==null)returntrue;Queue<Node>q=newQueue<Node>();q.Enqueue(root);boolend=false;while(q.Count>0){Nodecurrent=q.Dequeue();// Check left childif(current.left!=null){if(end)returnfalse;q.Enqueue(current.left);}else{// If left child is missing, // mark the endend=true;}// Check right childif(current.right!=null){if(end)returnfalse;q.Enqueue(current.right);}else{// If right child is missing,// mark the endend=true;}}returntrue;}staticvoidMain(string[]args){// Representation of Input tree// 1// / \// 2 3// / \ /// 4 5 6Noderoot=newNode(1);root.left=newNode(2);root.right=newNode(3);root.left.left=newNode(4);root.left.right=newNode(5);root.right.left=newNode(6);if(isCompleteBinaryTree(root))Console.WriteLine("True");elseConsole.WriteLine("False");}}
JavaScript
// JavaScript implementation to check if a // Binary Tree is completeclassNode{constructor(x){this.data=x;this.left=null;this.right=null;}}// Function to check if the binary// tree is completefunctionisCompleteBinaryTree(root){if(root===null)returntrue;constq=[];q.push(root);letend=false;while(q.length>0){constcurrent=q.shift();// Check left childif(current.left){if(end)returnfalse;q.push(current.left);}else{// If left child is missing,// mark the endend=true;}// Check right childif(current.right){if(end)returnfalse;q.push(current.right);}else{// If right child is missing,// mark the endend=true;}}returntrue;}// Representation of Input tree// 1// / \// 2 3// / \ /// 4 5 6constroot=newNode(1);root.left=newNode(2);root.right=newNode(3);root.left.left=newNode(4);root.left.right=newNode(5);root.right.left=newNode(6);if(isCompleteBinaryTree(root))console.log("True");elseconsole.log("False");
Output
True
[Expected Approach - 2] Checking position of NULL - O(n) Time and O(n) Space
A simple idea would be to check whether the NULL Node encountered is the last node of the Binary Tree. If the null node encountered in the binary tree is the last node then it is a complete binary tree and if there exists a valid node even after encountering a null node then the tree is not a complete binary tree.
Below is the implementation of the above approach:
C++
// C++ implementation to check if a binary// tree is complete by position of null#include<bits/stdc++.h>usingnamespacestd;classNode{public:intdata;Node*left;Node*right;Node(intx){data=x;left=nullptr;right=nullptr;}};// Function to check if the binary// tree is completeboolisCompleteBinaryTree(Node*root){if(root==nullptr){returntrue;}queue<Node*>q;q.push(root);boolnullEncountered=false;while(!q.empty()){Node*curr=q.front();q.pop();if(curr==NULL){// If we have seen a NULL node, we // set the flag to truenullEncountered=true;}else{// If that NULL node is not the last node then// return falseif(nullEncountered==true){returnfalse;}// Push both nodes even if // there are nullq.push(curr->left);q.push(curr->right);}}returntrue;}intmain(){// Representation of Input tree// 1// / \ // 2 3// / \ /// 4 5 6Node*root=newNode(1);root->left=newNode(2);root->right=newNode(3);root->left->left=newNode(4);root->left->right=newNode(5);root->right->left=newNode(6);if(isCompleteBinaryTree(root)){cout<<"True"<<endl;}else{cout<<"False"<<endl;}return0;}
Java
// Java implementation to check if a binary // tree is complete by position of nullimportjava.util.*;classNode{intdata;Nodeleft;Noderight;Node(intx){data=x;left=null;right=null;}}classGfG{// Function to check if the binary tree// is completestaticbooleanisCompleteBinaryTree(Noderoot){if(root==null){returntrue;}Queue<Node>q=newLinkedList<>();q.add(root);booleannullEncountered=false;while(!q.isEmpty()){Nodecurr=q.poll();if(curr==null){// If we have seen a null node,// we set the flagnullEncountered=true;}else{// If that null node is not the // last node, return falseif(nullEncountered){returnfalse;}// Push both nodes even if // there are nullq.add(curr.left);q.add(curr.right);}}returntrue;}publicstaticvoidmain(String[]args){// Representation of Input tree// 1// / \// 2 3// / \ /// 4 5 6Noderoot=newNode(1);root.left=newNode(2);root.right=newNode(3);root.left.left=newNode(4);root.left.right=newNode(5);root.right.left=newNode(6);if(isCompleteBinaryTree(root)){System.out.println("True");}else{System.out.println("False");}}}
Python
# Python implementation to check if a binary # tree is complete by position of nullclassNode:def__init__(self,x):self.data=xself.left=Noneself.right=None# Function to check if the binary# tree is completedefisCompleteBinaryTree(root):ifrootisNone:returnTruequeue=[]queue.append(root)nullEncountered=Falsewhilequeue:curr=queue.pop(0)ifcurrisNone:# If we have seen a null node, # set the flagnullEncountered=Trueelse:# If that null node is not the last# node, return falseifnullEncountered:returnFalse# Push both nodes even if # there are nullqueue.append(curr.left)queue.append(curr.right)returnTrueif__name__=='__main__':# Representation of Input tree# 1# / \# 2 3# / \ /# 4 5 6root=Node(1)root.left=Node(2)root.right=Node(3)root.left.left=Node(4)root.left.right=Node(5)root.right.left=Node(6)ifisCompleteBinaryTree(root):print("True")else:print("False")
C#
// C# implementation to check if a binary // tree is complete by position of nullusingSystem;usingSystem.Collections.Generic;classNode{publicintdata;publicNodeleft;publicNoderight;publicNode(intx){data=x;left=null;right=null;}}classGfG{// Function to check if the binary tree// is completestaticboolisCompleteBinaryTree(Noderoot){if(root==null){returntrue;}Queue<Node>q=newQueue<Node>();q.Enqueue(root);boolnullEncountered=false;while(q.Count>0){Nodecurr=q.Dequeue();if(curr==null){// If we have seen a null node, // set the flagnullEncountered=true;}else{// If that null node is not the // last node, return falseif(nullEncountered){returnfalse;}// Push both nodes even if there// are nullq.Enqueue(curr.left);q.Enqueue(curr.right);}}returntrue;}staticvoidMain(){// Representation of Input tree// 1// / \// 2 3// / \ /// 4 5 6Noderoot=newNode(1);root.left=newNode(2);root.right=newNode(3);root.left.left=newNode(4);root.left.right=newNode(5);root.right.left=newNode(6);if(isCompleteBinaryTree(root)){Console.WriteLine("True");}else{Console.WriteLine("False");}}}
JavaScript
// JavaScript implementation to check if a binary // tree is complete by position of nullclassNode{constructor(x){this.data=x;this.left=null;this.right=null;}}// Function to check if the binary// tree is completefunctionisCompleteBinaryTree(root){if(root===null){returntrue;}constqueue=[];queue.push(root);letnullEncountered=false;while(queue.length>0){constcurr=queue.shift();if(curr===null){// If we have seen a null node,// set the flagnullEncountered=true;}else{// If that null node is not the last // node, return falseif(nullEncountered){returnfalse;}// Push both nodes even if // there are nullqueue.push(curr.left);queue.push(curr.right);}}returntrue;}// Representation of Input tree// 1// / \// 2 3// / \ /// 4 5 6constroot=newNode(1);root.left=newNode(2);root.right=newNode(3);root.left.left=newNode(4);root.left.right=newNode(5);root.right.left=newNode(6);if(isCompleteBinaryTree(root)){console.log("True");}else{console.log("False");}