Count Complete Tree Nodes
MediumBinary Search
Decode it first. Clues, constraints, pattern and plan before any code.Given the root of a complete binary tree, return how many nodes it has. In a complete tree every level is full except possibly the last, and the last level's nodes are packed as far left as possible. Aim for better than visiting every node.
Trees in the examples are written in level order (top to bottom, left to right), the same way the tests build them.
[4, 8, 1, 6, 3] → 5
[10, 20, 30, 40, 50, 60, 70] → 7
[9, 2] → 2Constraints
0 <= number of nodes <= 5 * 10^40 <= Node.val <= 5 * 10^4- The tree is complete.