coding•DSA & Problem Solving•Trees & Binary Search Trees
Count Complete Tree Nodes
Problem Statement
Given the root of a **complete** binary tree, return the number of the nodes in the 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 `2^h` nodes inclusive at the last level `h`.
Design an algorithm that runs in less than `O(n)` time complexity.
### Constraints
- The number of nodes in the tree is in the range `[0, 5 * 10^4]`.
- `0 <= Node.val <= 5 * 10^4`
- The tree is guaranteed to be **complete**.
### Input Format
A single line containing space-separated integers representing the tree in level-order (-1 for null).
### Output Format
Print a single integer representing the total node count.
Sample Cases
Example 1Sample Case
Input: 1 2 3 4 5 6
Output: 6
Example 2Sample Case
Input: -1
Output: 0
Example 3Sample Case
Input: -1 -2 -3 -4 -5 -6
Output: 0
Constraints
Runtime Limit: 2000 ms per test batch
Memory Limit: 256 MB
Standard I/O stream reading
Official submissions are evaluated against hidden test cases on the server to verify time & memory complexity.