Example - small complete tree
Input
root = build_tree([1, 2, 3, 4, 5, 6])\ncount_nodes(root)
Output
6
The tree has nodes with values 1..6 arranged in level order; total count is 6.
Full lesson preview
Given the root of a complete binary tree, return the total number of nodes efficiently.
Problem statement
Task
Examples
Input
root = build_tree([1, 2, 3, 4, 5, 6])\ncount_nodes(root)
Output
6
The tree has nodes with values 1..6 arranged in level order; total count is 6.
Input format
Output format
Constraints
Samples
Input
count_nodes(build_tree([1, 2, 3, 4, 5, 6, 7]))
Output
7
A perfect tree of height 3 (levels 0..2) has 7 nodes.