Count Complete Tree Nodes

Given the root of a complete binary tree, return the total number of nodes in the tree. Pattern focus: Recursive Tree Metrics. Use the structure of a complete tree to count nodes efficiently.

Input Format

root = complete binary tree root

Output Format

total number of nodes in the tree

Constraints

  • 1 <= number of nodes <= 10^5
  • -10^4 <= node values <= 10^4
  • Input must satisfy the format described in inputFormat.

Examples

Example 1:

Input:

root = [1,2,3,4,5,6]

Output:

6

Explanation:

The tree contains six nodes.

Example 2:

Input:

root = [1,2,3,4,5,6,7]

Output:

7

Explanation:

A perfect complete tree of height 3 has 7 nodes.

Loading...
Count Complete Tree Nodes - Binary Tree