VOOZH about

URL: https://www.geeksforgeeks.org/dsa/check-if-a-given-binary-tree-is-complete-tree-or-not/

⇱ Check whether a given Binary Tree is Complete or not (Iterative Solution) - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

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

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


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:


Output
True
Comment
Article Tags:
Article Tags: