Two leaves
Input
count_leaves(TreeNode(1, TreeNode(2), TreeNode(3)))
Output
2
Nodes 2 and 3 are leaves.
Full lesson preview
Count how many leaf nodes (nodes with no children) are present in a binary tree.
Problem statement
Task
Examples
Input
count_leaves(TreeNode(1, TreeNode(2), TreeNode(3)))
Output
2
Nodes 2 and 3 are leaves.
Input format
Output format
Constraints
Samples
Input
count_leaves(None)
Output
0
Empty tree has zero leaves.