Check completeness of a binary tree

problem [https://leetcode.com/problems/check-completeness-of-a-binary-tree/ 958. Check Completeness of a Binary Tree].

Problem description
Given the  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  and   nodes inclusive at the last level.


 * 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.
 * 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.
 * Explanation: The node with value 7 isn't as far left as possible.


 * Constraints
 * The number of nodes in the tree is in the range.

Analysis
We can follow the similar procedure in Binary tree level-order traversal. But, instead of putting all nodes into the output array, we just need to use a  pointer to check if we have encountered a   node but later a valid node. If this happens then we return false. Otherwise, if we have traversed the entire tree, return true.

Time complexity
The time complexity to traverse the entire tree is O(n).

Space compelexity

 * Stack
 * The space complexity on stack is O(1), as we did not use recursion to go over each level.


 * Heap
 * We can use BFS to do this. The worst case is a perfect binary tree: layer 1 has 1 node, layer 2 has 2 nodes, layer 3 has 4 nodes, layer 4 has 8 nodes, …, and layer k has $$2^{k-1}$$ nodes. In other words, the maximum number of elements in the queue is one half of the total number of nodes in the tree.
 * So the space complexity on heap is O(n).

So total space complexity is O(n).