AlgoFrames

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