Efficiently count nodes in a complete binary tree using height comparisons.
Constraints
Example
>>> # Complete tree: root = {'val':1, 'left':{'val':2, 'left':{'val':4}, 'right':{'val':5}}, 'right':{'val':3, 'left':{'val':6}, 'right':None}}
>>> count_nodes(root)
6
>>> # Single node tree
>>> count_nodes({'val':1})
1
>>> # More examples:
>>> root = {'val':1, 'left':{'val':2}, 'right':{'val':3}}
>>> count_nodes(root)
3
Recent Submissions
No submissions yet — hit Run Tests to try!
Hints