Given a binary tree, check if it is a complete binary tree or not.

A complete binary tree is a binary tree in which every level, except possibly the last, is filled, and all nodes are as far left as possible. For example, the following binary trees are complete:

Complete Binary Tree

Practice this problem

1. Level Order Traversal (BFS)

We can modify level order traversal to check if a given binary tree is a complete binary tree or not. The idea is for every dequeued node, check if it is a full node (have both left and right children). If a node is found that is not a full node, i.e., either it has no children or only one child, then all the remaining nodes in the queue should not have any children. If anyone has a child, then it’s not a complete binary tree; otherwise, it is.

The algorithm can be implemented as follows in C++, Java, and Python:

C++


Download  Run Code

Output:

Complete binary tree

Java


Download  Run Code

Output:

Complete binary tree

Python


Download  Run Code

Output:

Complete binary tree

The time complexity of the above solution is O(n) and requires O(n) extra space, where n is the size of the binary tree.

2. Array representation of a complete tree

We can solve this problem by using the properties of a complete binary tree. We know that in the array representation of a binary tree, the left child for a node at index i is present at index 2i+1, and the right child is present at index 2i+2. If we construct an array with all the tree elements at the corresponding positions, then the elements will hold consecutive positions for a complete binary tree. If any vacant position is found, then the tree cannot be complete.

 
Complete Binary Trees

Following is the implementation in C++, Java, and Python based on the above idea:

C++


Download  Run Code

Output:

Complete binary tree

Java


Download  Run Code

Output:

Complete binary tree

Python


Download  Run Code

Output:

Complete binary tree

The time complexity of the above solution is O(n) and requires O(n) extra space, where n is the size of the binary tree.

3. Space-optimized previous Approach

The above approach takes extra space for storage of the boolean array. As discussed for a complete binary tree, the left and right child’s index for any node is less than the total number of nodes for every node. We can avoid using extra space by passing the index as a recursion parameter and checking for every node that their left and right child’s index are within the correct range.

Complete Binary Tree – Array Representation

Following is the C++, Java, and Python program that demonstrates it:

C++


Download  Run Code

Output:

Complete binary tree

Java


Download  Run Code

Output:

Complete binary tree

Python


Download  Run Code

Output:

Complete binary tree

The time complexity of the above solution is O(n), where n is the total number of nodes in the binary tree. The program requires O(h) extra space for the call stack, where h is the height of the tree.