PIXELBANKv9.1.0
Menu

Count Complete Tree Nodes

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,6 represents 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 66.

Constraints:

  • 0 <= number of nodes <= 5 * 10^4
  • 0 <= Node.val <= 5 * 10^4
🔒

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.