Given the root of a binary tree, determine if it is a complete binary tree.

In a complete binary tree, every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as possible. It can have between 1 and 2h nodes inclusive at the last level h.

&nbsp;
Example 1:

Input: root = [1,2,3,4,5,6]
Output: true
Explanation: Every level before the last is full (ie. levels with node-values {1} and {2, 3}), and all nodes in the last level ({4, 5, 6}) are as far left as possible.


Example 2:

Input: root = [1,2,3,4,5,null,7]
Output: false
Explanation: The node with value 7 isn't as far left as possible.


&nbsp;
Constraints:


	The number of nodes in the tree is in the range [1, 100].
	1 &lt;= Node.val &lt;= 1000


class Solution:
	def isCompleteTree(self, root: Optional[TreeNode]) -> bool:
		# if not root: return True
		def node_count(root):
			if not root: return 0
			return 1 + node_count(root.left) + node_count(root.right)

		def isCBT(root,i,count):
			if not root: return True
			if i>=count: return False
			return isCBT(root.left,2*i+1,count) and isCBT(root.right,2*i+2,count)


		return isCBT(root,0,node_count(root))
class Solution {
    public boolean isCompleteTree(TreeNode root) {
        boolean end = false;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()) {
            TreeNode currentNode = queue.poll();
            if(currentNode == null) {
                end = true;
            } else {
                if(end) {
                    return false;
                }
                queue.offer(currentNode.left);
                queue.offer(currentNode.right);
            }
        }
        return true;
    }
}
class Solution {
public:
    bool isComplete = true;

    // Returns min, max height of node
    pair<int,int> helper(TreeNode *root){
        // NULL is of height zero
        if(root == NULL){
            return {0, 0};
        }

        // Moving to its children
        pair<int,int> lst = helper(root->left);
        pair<int,int> rst = helper(root->right);

        // Height of rst is greater than lst or
        // There are levels which are not filled other than the last level (diff between levels > 1)
        if(rst.second > lst.first || lst.second - rst.first > 1){
            isComplete = false;
        }

        return {min(lst.first, rst.first)+1, max(lst.second, rst.second)+1};
    }

    bool isCompleteTree(TreeNode* root) {
        helper(root);
        return isComplete;
    }
};
var isCompleteTree = function(root) {
let queue = [root];
let answer = [root];
while(queue.length > 0) {
    let current = queue.shift();
    if(current) {
        queue.push(current.left);
        answer.push(current.left);
        queue.push(current.right);
        answer.push(current.right);
    }
}

console.log("queue", queue);

while(answer.length > 0 ) {
    let current = answer.shift();
    if(current === null) break;
}
console.log("answer after shifting", answer);
 let flag = true;
 for(let current of answer) {
    if(current !== null) {
        flag = false;
        break
    }
}

return flag
};
Check Completeness of a Binary Tree