Count the leaves
Standard · Trace the algorithm · about 5 min
A leaf is a node with no children. Count the leaves of the tree below, then follow the trace as it checks every node.
Example 1
Input:
{"levelOrder":[1,2,3,4,5,null,6,null,null,7,8]}
Output:
4
The leaves are 4, 7, 8 and 6.
Constraints
- The trace uses the tree shown
Learn the idea first: Trees: root, leaf, depth, height, subtree, a visual lesson.
More Trees: root, leaf, depth, height, subtree problems
- Label the parts of a tree Warm-up
- Compute the height Warm-up
- Height of a binary tree Standard
- Count the leaves in code Standard
- Count the nodes at depth d Standard
- Nodes at depth d in a full tree Challenge