Check whether a given Binary Tree is Complete or not (Iterative Solution) (original) (raw)

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:

check-whether-a-given-binary-tree-is-complete-or-not

The following trees are examples of Non-Complete Binary Trees:

check-whether-a-given-binary-tree-is-complete-or-not-2

Try It Yourselfredirect icon

Table of Content

[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.

12

Below is the implementation of the above approach:

C++ `

// C++ implementation to check if a // Binary Tree is complete #include <bits/stdc++.h> using namespace std;

class Node { public: int data; Node* left; Node* right;

Node(int x) {
    data = x;
    left = nullptr;
    right = nullptr;
}

};

// Function to check if the binary tree is complete bool isCompleteBinaryTree(Node* root) { if (root == nullptr) return true;

queue<Node*> q;
q.push(root);
bool end = false; 

while (!q.empty()) {
    Node* current = q.front();
    q.pop();

    // Check left child
    if (current->left) {
        if (end) 
            return false;
        q.push(current->left);
    } 
   else {
     
        // If left child is missing,
         // mark the end
        end = true;
    }

    // Check right child
    if (current->right) {
        if (end) 
            return false;
      
        q.push(current->right);
    } 
   else {
     
        // If right child is missing,
         // mark the end
        end = true;
    }
}

return true; 

}

int main() {

// Representation of Input tree
//         1
//        /  \
//       2    3
//      / \   /
//     4   5  6
Node* root = new Node(1);
root->left = new Node(2);
root->right = new Node(3);
root->left->left = new Node(4);
root->left->right = new Node(5);
root->right->left = new Node(6); 

if (isCompleteBinaryTree(root))
    cout << "True" << endl;
else
    cout << "False" << endl;

return 0;

}

Java

// Java implementation to check if a // Binary Tree is complete import java.util.LinkedList; import java.util.Queue;

class Node { int data; Node left; Node right;

Node(int x) {
    data = x;
    left = null;
    right = null;
}

}

class GfG {

// Function to check if the binary
  // tree is complete
static boolean isCompleteBinaryTree(Node root) {
    if (root == null)
        return true;

    Queue<Node> q = new LinkedList<>();
    q.add(root);
    boolean end = false; 

    while (!q.isEmpty()) {
        Node current = q.poll();

        // Check left child
        if (current.left != null) {
            if (end) 
                return false;
            q.add(current.left);
        } 
        else {
          
            // If left child is missing,
              // mark the end
            end = true;
        }

        // Check right child
        if (current.right != null) {
            if (end) 
                return false;
            q.add(current.right);
        } 
        else {
          
            // If right child is missing,
              // mark the end
            end = true;
        }
    }

    return true; 
}

public static void main(String[] args) {
  
    // Representation of Input tree
    //         1
    //        /  \
    //       2    3
    //      / \   /
    //     4   5  6
    Node root = new Node(1);
    root.left = new Node(2);
    root.right = new Node(3);
    root.left.left = new Node(4);
    root.left.right = new Node(5);
    root.right.left = new Node(6); 
    
    if (isCompleteBinaryTree(root))
        System.out.println("True");
    else
        System.out.println("False");
}

}

Python

Python implementation to check if a

Binary Tree is complete

from collections import deque

class Node: def init(self, x): self.data = x self.left = None self.right = None

Function to check if the binary

tree is complete

def is_complete_binary_tree(root): if root is None: return True

q = deque([root])
end = False 

while q:
    current = q.popleft()

    # Check left child
    if current.left:
        if end:
            return False
        q.append(current.left)
    else:
      
        # If left child is missing,
        # mark the end
        end = True

    # Check right child
    if current.right:
        if end:
            return False
        q.append(current.right)
    else:
      
        # If right child is missing, 
        # mark the end
        end = True

return True 

if name == "main":

# Representation of Input tree
#         1
#        /  \
#       2    3
#      / \   /
#     4   5  6
root = 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) 

if is_complete_binary_tree(root):
    print("True")
else:
    print("False")

C#

// C# implementation to check if a // Binary Tree is complete using System; using System.Collections.Generic;

class Node { public int data; public Node left; public Node right;

public Node(int x) {
    data = x;
    left = null;
    right = null;
}

}

// Function to check if the binary // tree is complete class GfG { static bool isCompleteBinaryTree(Node root) { if (root == null) return true;

    Queue<Node> q = new Queue<Node>();
    q.Enqueue(root);
    bool end = false; 

    while (q.Count > 0) {
        Node current = q.Dequeue();

        // Check left child
        if (current.left != null) {
            if (end)
                return false;
            q.Enqueue(current.left);
        } 
        else {
          
            // If left child is missing, 
              // mark the end
            end = true;
        }

        // Check right child
        if (current.right != null) {
            if (end)
                return false;
            q.Enqueue(current.right);
        } 
        else {
          
            // If right child is missing,
              // mark the end
            end = true;
        }
    }

    return true; 
}

static void Main(string[] args) {
  
    // Representation of Input tree
    //         1
    //        /  \
    //       2    3
    //      / \   /
    //     4   5  6
    Node root = new Node(1);
    root.left = new Node(2);
    root.right = new Node(3);
    root.left.left = new Node(4);
    root.left.right = new Node(5);
    root.right.left = new Node(6); 
    
    if (isCompleteBinaryTree(root))
        Console.WriteLine("True");
    else
        Console.WriteLine("False");
}

}

JavaScript

// JavaScript implementation to check if a // Binary Tree is complete class Node { constructor(x) { this.data = x; this.left = null; this.right = null; } }

// Function to check if the binary // tree is complete function isCompleteBinaryTree(root) { if (root === null) return true;

const q = [];
q.push(root);
let end = false; 

while (q.length > 0) {
    const current = q.shift();

    // Check left child
    if (current.left) {
        if (end)
            return false;
        q.push(current.left);
    } 
    else {
    
        // If left child is missing,
        // mark the end
        end = true;
    }

    // Check right child
    if (current.right) {
        if (end)
            return false;
        q.push(current.right);
    } 
    else {
    
        // If right child is missing,
        // mark the end
        end = true;
    }
}

return true; 

}

// Representation of Input tree // 1 // /
// 2 3 // / \ / // 4 5 6 const root = new Node(1); root.left = new Node(2); root.right = new Node(3); root.left.left = new Node(4); root.left.right = new Node(5); root.right.left = new Node(6);

if (isCompleteBinaryTree(root)) console.log("True"); else console.log("False");

`

**[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> using namespace std;

class Node { public: int data; Node* left; Node* right;

Node(int x) {
    data = x;
    left = nullptr;
    right = nullptr;
}

};

// Function to check if the binary // tree is complete bool isCompleteBinaryTree(Node* root) { if (root == nullptr) { return true; }

queue<Node*> q;
q.push(root);
bool nullEncountered = 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 true
        nullEncountered= true;
    }
    else {
      
        // If that NULL node is not the last node then
        // return false
        if (nullEncountered == true) {
            return false;
        }
      
        // Push both nodes even if 
          // there are null
        q.push(curr->left);
        q.push(curr->right);
    }      
}

return true;

}

int main() {

// Representation of Input tree
//         1
//        /  \
//       2    3
//      / \   /
//     4   5  6
Node* root = new Node(1);
root->left = new Node(2);
root->right = new Node(3);
root->left->left = new Node(4);
root->left->right = new Node(5);
root->right->left = new Node(6); 

if (isCompleteBinaryTree(root)) {
    cout << "True" << endl;
} 
else {
    cout << "False" << endl;
}

return 0;

}

Java

// Java implementation to check if a binary // tree is complete by position of null import java.util.*;

class Node { int data; Node left; Node right;

Node(int x) {
    data = x;
    left = null;
    right = null;
}

}

class GfG {

// Function to check if the binary tree
  // is complete
static boolean isCompleteBinaryTree(Node root) {
    if (root == null) {
        return true;
    }

    Queue<Node> q = new LinkedList<>();
    q.add(root);
    boolean nullEncountered = false;

    while (!q.isEmpty()) {
        Node curr = q.poll();

        if (curr == null) {
          
            // If we have seen a null node,
              // we set the flag
            nullEncountered = true;
        } 
        else {
          
            // If that null node is not the 
              // last node, return false
            if (nullEncountered) {
                return false;
            }
          
            // Push both nodes even if 
              // there are null
            q.add(curr.left);
            q.add(curr.right);
        }
    }

    return true;
}

public static void main(String[] args) {
  
    // Representation of Input tree
    //         1
    //        /  \
    //       2    3
    //      / \   /
    //     4   5  6
    Node root = new Node(1);
    root.left = new Node(2);
    root.right = new Node(3);
    root.left.left = new Node(4);
    root.left.right = new Node(5);
    root.right.left = new Node(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 null

class Node: def init(self, x): self.data = x self.left = None self.right = None

Function to check if the binary

tree is complete

def isCompleteBinaryTree(root): if root is None: return True

queue = []
queue.append(root)
nullEncountered = False

while queue:
    curr = queue.pop(0)

    if curr is None:
      
        # If we have seen a null node, 
        # set the flag
        nullEncountered = True
    else:
      
        # If that null node is not the last
        # node, return false
        if nullEncountered:
            return False
        
        # Push both nodes even if 
        # there are null
        queue.append(curr.left)
        queue.append(curr.right)

return True

if name == 'main':

Representation of Input tree

1

/ \

2 3

/ \ /

4 5 6

root = 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)

if isCompleteBinaryTree(root): print("True") else: print("False")

C#

// C# implementation to check if a binary // tree is complete by position of null using System; using System.Collections.Generic;

class Node { public int data; public Node left; public Node right;

public Node(int x) {
    data = x;
    left = null;
    right = null;
}

}

class GfG {

// Function to check if the binary tree
  // is complete
static bool isCompleteBinaryTree(Node root) {
    if (root == null) {
        return true;
    }

    Queue<Node> q = new Queue<Node>();
    q.Enqueue(root);
    bool nullEncountered = false;

    while (q.Count > 0) {
        Node curr = q.Dequeue();

        if (curr == null) {
          
            // If we have seen a null node, 
              // set the flag
            nullEncountered = true;
        } else {
          
            // If that null node is not the 
              // last node, return false
            if (nullEncountered) {
                return false;
            }

            // Push both nodes even if there
              // are null
            q.Enqueue(curr.left);
            q.Enqueue(curr.right);
        }
    }

    return true;
}

static void Main() {
  
    // Representation of Input tree
    //         1
    //        /  \
    //       2    3
    //      / \   /
    //     4   5  6
    Node root = new Node(1);
    root.left = new Node(2);
    root.right = new Node(3);
    root.left.left = new Node(4);
    root.left.right = new Node(5);
    root.right.left = new Node(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 null class Node { constructor(x) { this.data = x; this.left = null; this.right = null; } }

// Function to check if the binary // tree is complete function isCompleteBinaryTree(root) { if (root === null) { return true; }

const queue = [];
queue.push(root);
let nullEncountered = false;

while (queue.length > 0) {
    const curr = queue.shift();

    if (curr === null) {
    
        // If we have seen a null node,
        // set the flag
        nullEncountered = true;
    }
    else {
    
        // If that null node is not the last 
        // node, return false
        if (nullEncountered) {
            return false;
        }

        // Push both nodes even if 
        // there are null
        queue.push(curr.left);
        queue.push(curr.right);
    }
}

return true;

}

// Representation of Input tree // 1 // /
// 2 3 // / \ / // 4 5 6 const root = new Node(1); root.left = new Node(2); root.right = new Node(3); root.left.left = new Node(4); root.left.right = new Node(5); root.right.left = new Node(6);

if (isCompleteBinaryTree(root)) { console.log("True"); } else { console.log("False"); }

`