bool isCompleteTree(struct node * root,unsigned int i ,unsigned int size) { if(root == NULL) return true; if(i >= size) return false; return (isCompleteTree(root->left,2*i+1 , size) && isCompleteTree(root->right,2*i+2,size)); }
int countNodes(TreeNode* root) { int cnt = isCompleteTree(root); if (cnt != -1) return cnt; int leftCnt = countNodes(root->left); int rightCnt = countNodes(root->right); return leftCnt + rightCnt + 1; }
// 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); //root->right->right->right = newNode(67); unsigned int node_count = countNodes(root); unsigned int index = 0; if (isCompleteTree(root, index, node_count)) printf("The Binary Tree is complete\n"); else printf("The Binary Tree is not complete\n"); return (0); }