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.
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.
The Binary Tree is not complete
This article is contributed by Gaurav Gupta. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Check whether a given Binary Tree is Complete or not | Set 1 (Iterative Solution)
- Check whether a binary tree is a full binary tree or not
- Check whether a binary tree is a full binary tree or not | Iterative Approach
- Check whether a given binary tree is skewed binary tree or not?
- Check whether a given binary tree is perfect or not
- Check if value exists in level-order sorted complete binary tree
- Non-recursive program to delete an entire binary tree
- Count half nodes in a Binary tree (Iterative and Recursive)
- Count full nodes in a Binary tree (Iterative and Recursive)
- Recursive Program to Print extreme nodes of each level of Binary Tree in alternate order
- Check if a binary tree is subtree of another binary tree | Set 1
- Check if a binary tree is subtree of another binary tree | Set 2
- Check whether nodes of Binary Tree form Arithmetic, Geometric or Harmonic Progression
- Check whether every node of binary tree has a value K on itself or its any immediate neighbours
- Linked complete binary tree & its creation
- Construct Complete Binary Tree from its Linked List Representation
- Construct a complete binary tree from given array in level order fashion
- Height of a complete binary tree (or Heap) with N nodes
- Find the largest Complete Subtree in a given Binary Tree
- Iterative Boundary Traversal of Complete Binary tree