Check if given binary tree is complete binary tree or not
A complete binary tree is a binary tree in which all levels except the last are completely filled, and all nodes in the last level are leftjustified. In other words, a complete binary tree is a binary tree where every level is completely filled, except possibly the last level, and all nodes are as far left as possible.
To check if a given binary tree is a complete binary tree or not, we can follow the below steps:
 Perform a level order traversal of the binary tree.
 For each node encountered during the traversal, mark its left and right children as present and enqueue them.
 If we encounter a null node, mark its left and right children as absent.
 After the traversal, if we encounter a node whose left child is marked as absent, but the right child is present, or if we encounter a node whose left child is present, but the right child is absent or marked as absent, then the tree is not a complete binary tree.
If we do not encounter such a node during the traversal, then the given binary tree is a complete binary tree.
Code Example

1) Check for complete binary tree in java
2) Check for complete binary tree in c++
3) Check for complete binary tree in c
4) Check for complete binary tree in c#
5) Check for complete binary tree in vb.net
6) Check for complete binary tree in php
7) Check for complete binary tree in node js
8) Check for complete binary tree in typescript
9) Check for complete binary tree in python
10) Check for complete binary tree in ruby
11) Check for complete binary tree in scala
12) Check for complete binary tree in swift
13) Check for complete binary tree in kotlin
14) Check for complete binary tree in golang
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment