99 questions/Solutions/61

From HaskellWiki
< 99 questions‎ | Solutions
Revision as of 00:06, 14 July 2010 by Wapcaplet (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Count the leaves of a binary tree

A leaf is a node with no successors. Write a predicate count_leaves/2 to count them.

count_leaves  Empty                 = 0
count_leaves (Branch a Empty Empty) = 1
count_leaves (Branch a left  right) = count_leaves left + count_leaves right