Given the root of a complete binary tree, return the number of the nodes in the tree. According to Wikipedia, every level, except possibly the last, is completely filled in a complete binary tree, and all nodes in the last level are as far left as possible. It can have between 1 and 2^h nodes inclusive at the last level h. Design an algorithm that runs in less than O(n) time complexity.
Example 1
Input: root = [1,2,3,4,5,6]
Output: 6
Explanation: The first two levels are full (1 + 2 = 3 nodes), and the last level has 3 nodes filled from the left (4, 5, 6). The total is 6.
Example 2
Input: root = []
Output: 0
Explanation: The tree is empty, so it has 0 nodes.
Example 3
Input: root = [1]
Output: 1
Explanation: The tree has only the root, so it has 1 node.
For a complete tree, walk the leftmost and rightmost spines. If their heights are equal, the tree is perfectly filled and has exactly 2^h - 1 nodes — computed instantly. Otherwise, recurse into both children; the completeness guarantees one side is always perfect, giving an O(log^2 n) algorithm.
Run checks all cases above. Submit evaluates all test cases.