A complete binary tree is defined as a binary tree in which every level, except possibly the deepest, is completely filled. At deepest level, all nodes must be as far left a
You can also solve this problem by using level order traversal. The procedure is as follows:
Here is a c++ code:
My tree node is:
struct node{
int data;
node *left, *right;
};
void checkcomplete(){//checks whether a tree is complete or not by performing level order traversal
node *curr = root;
queue Q;
vector arr;
int lastentry = 0;
Q.push(curr);
int currlevel = 1, nextlevel = 0;
while( currlevel){
node *temp = Q.front();
Q.pop();
currlevel--;
if(temp){
arr.push_back(temp->data);
lastentry = arr.size();
Q.push(temp->left);
Q.push(temp->right);
nextlevel += 2;
}else
arr.push_back(INT_MIN);
if(!currlevel){
currlevel = nextlevel;
nextlevel = 0;
}
}
int flag = 0;
for( int i = 0; i