Check whether a binary tree is a complete tree or not | Set 2 (Recursive Solution)
Last Updated :
31 May, 2023
A complete binary tree is a binary tree whose all levels except the last level are completely filled and all the leaves in the last level are all to the left side. More information about complete binary trees can be found here.
Example:
Below tree is a Complete Binary Tree (All nodes till the second last nodes are filled and all leaves are to the left side)

An iterative solution for this problem is discussed in below post.
Check whether a given Binary Tree is Complete or not | Set 1 (Using Level Order Traversal)
In this post a recursive solution is discussed.
In the array representation of a binary tree, if the parent node is assigned an index of ‘i’ and left child gets assigned an index of ‘2*i + 1’ while the right child is assigned an index of ‘2*i + 2’. If we represent the above binary tree as an array with the respective indices assigned to the different nodes of the tree above from top to down and left to right.
Hence we proceed in the following manner in order to check if the binary tree is complete binary tree.
- Calculate the number of nodes (count) in the binary tree.
- Start recursion of the binary tree from the root node of the binary tree with index (i) being set as 0 and the number of nodes in the binary (count).
- If the current node under examination is NULL, then the tree is a complete binary tree. Return true.
- If index (i) of the current node is greater than or equal to the number of nodes in the binary tree (count) i.e. (i>= count), then the tree is not a complete binary. Return false.
- Recursively check the left and right sub-trees of the binary tree for same condition. For the left sub-tree use the index as (2*i + 1) while for the right sub-tree use the index as (2*i + 2).
The time complexity of the above algorithm is O(n). Following is the code for checking if a binary tree is a complete binary tree.
Implementation:
C++
/* C++ program to checks if a binary tree complete or not */
#include<bits/stdc++.h>
#include<stdbool.h>
using namespace std;
/* Tree node structure */
class Node
{
public:
int key;
Node *left, *right;
Node *newNode(char k)
{
Node *node = ( Node*)malloc(sizeof( Node));
node->key = k;
node->right = node->left = NULL;
return node;
}
};
/* Helper function that allocates a new node with the
given key and NULL left and right pointer. */
/* This function counts the number of nodes
in a binary tree */
unsigned int countNodes(Node* root)
{
if (root == NULL)
return (0);
return (1 + countNodes(root->left) +
countNodes(root->right));
}
/* This function checks if the binary tree
is complete or not */
bool isComplete ( Node* root, unsigned int index,
unsigned int number_nodes)
{
// An empty tree is complete
if (root == NULL)
return (true);
// If index assigned to current node is more than
// number of nodes in tree, then tree is not complete
if (index >= number_nodes)
return (false);
// Recur for left and right subtrees
return (isComplete(root->left, 2*index + 1, number_nodes) &&
isComplete(root->right, 2*index + 2, number_nodes));
}
// Driver code
int main()
{
Node n1;
// Let us create tree in the last diagram above
Node* root = NULL;
root = n1.newNode(1);
root->left = n1.newNode(2);
root->right = n1.newNode(3);
root->left->left = n1.newNode(4);
root->left->right = n1.newNode(5);
root->right->right = n1.newNode(6);
unsigned int node_count = countNodes(root);
unsigned int index = 0;
if (isComplete(root, index, node_count))
cout << "The Binary Tree is complete\n";
else
cout << "The Binary Tree is not complete\n";
return (0);
}
// This code is contributed by SoumikMondal
C
/* C program to checks if a binary tree complete or not */
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
/* Tree node structure */
struct Node
{
int key;
struct Node *left, *right;
};
/* Helper function that allocates a new node with the
given key and NULL left and right pointer. */
struct Node *newNode(char k)
{
struct Node *node = (struct Node*)malloc(sizeof(struct Node));
node->key = k;
node->right = node->left = NULL;
return node;
}
/* This function counts the number of nodes in a binary tree */
unsigned int countNodes(struct Node* root)
{
if (root == NULL)
return (0);
return (1 + countNodes(root->left) + countNodes(root->right));
}
/* This function checks if the binary tree is complete or not */
bool isComplete (struct Node* root, unsigned int index,
unsigned int number_nodes)
{
// An empty tree is complete
if (root == NULL)
return (true);
// If index assigned to current node is more than
// number of nodes in tree, then tree is not complete
if (index >= number_nodes)
return (false);
// Recur for left and right subtrees
return (isComplete(root->left, 2*index + 1, number_nodes) &&
isComplete(root->right, 2*index + 2, number_nodes));
}
// Driver program
int main()
{
// Le us create tree in the last diagram above
struct Node* root = NULL;
root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
root->right->right = newNode(6);
unsigned int node_count = countNodes(root);
unsigned int index = 0;
if (isComplete(root, index, node_count))
printf("The Binary Tree is complete\n");
else
printf("The Binary Tree is not complete\n");
return (0);
}
Java
// Java program to check if binary tree is complete or not
/* Tree node structure */
class Node
{
int data;
Node left, right;
Node(int item) {
data = item;
left = right = null;
}
}
class BinaryTree
{
Node root;
/* This function counts the number of nodes in a binary tree */
int countNodes(Node root)
{
if (root == null)
return (0);
return (1 + countNodes(root.left) + countNodes(root.right));
}
/* This function checks if the binary tree is complete or not */
boolean isComplete(Node root, int index, int number_nodes)
{
// An empty tree is complete
if (root == null)
return true;
// If index assigned to current node is more than
// number of nodes in tree, then tree is not complete
if (index >= number_nodes)
return false;
// Recur for left and right subtrees
return (isComplete(root.left, 2 * index + 1, number_nodes)
&& isComplete(root.right, 2 * index + 2, number_nodes));
}
// Driver program
public static void main(String args[])
{
BinaryTree tree = new BinaryTree();
// Le us create tree in the last diagram above
Node NewRoot = null;
tree.root = new Node(1);
tree.root.left = new Node(2);
tree.root.right = new Node(3);
tree.root.left.right = new Node(5);
tree.root.left.left = new Node(4);
tree.root.right.right = new Node(6);
int node_count = tree.countNodes(tree.root);
int index = 0;
if (tree.isComplete(tree.root, index, node_count))
System.out.print("The binary tree is complete");
else
System.out.print("The binary tree is not complete");
}
}
// This code is contributed by Mayank Jaiswal
Python3
# Python program to check if a binary tree complete or not
# Tree node structure
class Node:
# Constructor to create a new node
def __init__(self, key):
self.key = key
self.left = None
self.right = None
# This function counts the number of nodes in a binary tree
def countNodes(root):
if root is None:
return 0
return (1+ countNodes(root.left) + countNodes(root.right))
# This function checks if binary tree is complete or not
def isComplete(root, index, number_nodes):
# An empty is complete
if root is None:
return True
# If index assigned to current nodes is more than
# number of nodes in tree, then tree is not complete
if index >= number_nodes :
return False
# Recur for left and right subtrees
return (isComplete(root.left , 2*index+1 , number_nodes)
and isComplete(root.right, 2*index+2, number_nodes)
)
# Driver Program
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
root.right.right = Node(6)
node_count = countNodes(root)
index = 0
if isComplete(root, index, node_count):
print ("The Binary Tree is complete")
else:
print ("The Binary Tree is not complete")
# This code is contributed by Nikhil Kumar Singh(nickzuck_007)
C#
// C# program to check if binary
// tree is complete or not
using System;
/* Tree node structure */
class Node
{
public int data;
public Node left, right;
public Node(int item)
{
data = item;
left = right = null;
}
}
public class BinaryTree
{
Node root;
/* This function counts the number
of nodes in a binary tree */
int countNodes(Node root)
{
if (root == null)
return (0);
return (1 + countNodes(root.left) +
countNodes(root.right));
}
/* This function checks if the
binary tree is complete or not */
bool isComplete(Node root, int index,
int number_nodes)
{
// An empty tree is complete
if (root == null)
return true;
// If index assigned to current node is more than
// number of nodes in tree, then tree is not complete
if (index >= number_nodes)
return false;
// Recur for left and right subtrees
return (isComplete(root.left, 2 * index + 1, number_nodes)
&& isComplete(root.right, 2 * index + 2, number_nodes));
}
// Driver code
public static void Main()
{
BinaryTree tree = new BinaryTree();
// Let us create tree in the last diagram above
tree.root = new Node(1);
tree.root.left = new Node(2);
tree.root.right = new Node(3);
tree.root.left.right = new Node(5);
tree.root.left.left = new Node(4);
tree.root.right.right = new Node(6);
int node_count = tree.countNodes(tree.root);
int index = 0;
if (tree.isComplete(tree.root, index, node_count))
Console.WriteLine("The binary tree is complete");
else
Console.WriteLine("The binary tree is not complete");
}
}
/* This code is contributed by Rajput-Ji*/
JavaScript
<script>
// JavaScript program to check if
// binary tree is complete or not
/* Tree node structure */
class Node {
constructor(val) {
this.data = val;
this.left = null;
this.right = null;
}
}
var root;
/* This function counts the number of
nodes in a binary tree */
function countNodes(root) {
if (root == null)
return (0);
return (1 + countNodes(root.left) + countNodes(root.right));
}
/* This function checks if the binary tree is complete or not */
function isComplete(root , index , number_nodes) {
// An empty tree is complete
if (root == null)
return true;
// If index assigned to current node is more than
// number of nodes in tree, then tree is not complete
if (index >= number_nodes)
return false;
// Recur for left and right subtrees
return (isComplete(root.left, 2 * index + 1, number_nodes)
&& isComplete(root.right, 2 * index + 2, number_nodes));
}
// Driver program
// Le us create tree in the last diagram above
var NewRoot = null;
root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.right = new Node(5);
root.left.left = new Node(4);
root.right.right = new Node(6);
var node_count = countNodes(root);
var index = 0;
if (isComplete(root, index, node_count))
document.write("The binary tree is complete");
else
document.write("The binary tree is not complete");
// This code contributed by umadevi9616
</script>
OutputThe Binary Tree is not complete
Time Complexity: O(N) where N is the number of nodes in the tree.
Space Complexity: O(h) where h is the height of given tree due to recursion call.
Similar Reads
Check whether a given Binary Tree is Complete or not (Iterative Solution)
Given a Binary Tree, the task is to check whether the given Binary Tree is a 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. Examples:The following trees are examples
12 min read
Check whether a given binary tree is skewed binary tree or not?
Given a Binary Tree check whether it is skewed binary tree or not. A skewed tree is a tree where each node has only one child node or none. Examples: Input : 5 / 4 \ 3 / 2 Output : Yes Input : 5 / 4 \ 3 / \ 2 4 Output : No The idea is to check if a node has two children. If node has two children ret
13 min read
Check whether a binary tree is a full binary tree or not
A full binary tree is defined as a binary tree in which all nodes have either zero or two child nodes. Conversely, there is no node in a full binary tree, which has one child node. More information about full binary trees can be found here. For Example : Recommended PracticeFull binary treeTry It! T
14 min read
Check whether a given binary tree is perfect or not
Given a Binary Tree, the task is to check whether the given Binary Tree is a perfect Binary Tree or not.Note:A Binary tree is a Perfect Binary Tree in which all internal nodes have two children and all leaves are at the same level.A Perfect Binary Tree of height h has 2h â 1 nodes.Examples: Input:Ou
13 min read
Check whether a binary tree is a full binary tree or not | Iterative Approach
Given a binary tree containing n nodes. The problem is to check whether the given binary tree is a full binary tree or not. A full binary tree is defined as a binary tree in which all nodes have either zero or two child nodes. Conversely, there is no node in a full binary tree, which has only one ch
8 min read
Check if the given binary tree has a sub-tree with equal no of 1's and 0's | Set 2
Given a tree having every node's value as either 0 or 1, the task is to find whether the given binary tree contains any sub-tree that has equal number of 0's and 1's, if such sub-tree is found then print Yes else print No.Examples: Input: Output: Yes There are two sub-trees with equal number of 1's
10 min read
Simple Recursive solution to check whether BST contains dead end
Given a Binary Search Tree that contains positive integer values greater than 0. The task is to check whether the BST contains a dead end or not. Here Dead End means, we are not able to insert any integer element after that node. Examples: Input : 8 / \ 5 9 / \ 2 7 / 1 Output : Yes Explanation : Nod
10 min read
Check if a Binary Tree is an Even-Odd Tree or not
Given a Binary Tree, the task is to check if the binary tree is an Even-Odd binary tree or not. A Binary Tree is called an Even-Odd Tree when all the nodes which are at even levels have even values (assuming root to be at level 0) and all the nodes which are at odd levels have odd values. Examples:
15+ min read
Check if the given Binary Tree have a Subtree with equal no of 1's and 0's
Given a Binary Tree having data at nodes as either 0's or 1's. The task is to find out whether there exists a subtree having an equal number of 1's and 0's. Examples: Input : Output : True There are two subtrees present in the above tree where the number of 1's is equal to the number of 0's. Input :
10 min read
Check if a Binary Tree contains duplicate subtrees of size 2 or more
Given a Binary Tree, the task is to check whether the Binary tree contains a duplicate sub-tree of size 2 or more. Note: Two same leaf nodes are not considered as the subtree as the size of a leaf node is one.Example:Input: Output: TrueExplanation: Table of Content[Naive Approach] Generating All Sub
15 min read