📘
Count Complete Tree Nodes
EasyTrees & Graphs
Given the root of a complete binary tree (as a level-order array), return the number of nodes.
A complete binary tree has every level fully filled except possibly the last, which is filled from left to right.
Example:
Input:
1,2,3,4,5,6
Output:
6
Reasoning:
- The input array
1,2,3,4,5,6represents a level-order traversal of the complete binary tree. - We can visualize the tree as:
- Level 1: 1
- Level 2: 2, 3
- Level 3: 4, 5, 6
- Since it's a complete binary tree, every level is fully filled except possibly the last, which is filled from left to right.
- The number of nodes is equal to the number of elements in the input array, so the output is 6.
Constraints:
- 0 <= number of nodes <= 5 * 10^4
- 0 <= Node.val <= 5 * 10^4
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.