Difference between revisions of "99 questions/Solutions/61"
< 99 questions | Solutions
Jump to navigation
Jump to search
Line 4: | Line 4: | ||
<haskell> |
<haskell> |
||
− | + | countLeaves Empty = 0 |
|
− | + | countLeaves (Branch a Empty Empty) = 1 |
|
− | + | countLeaves (Branch a left right) = count_leaves left + count_leaves right |
|
</haskell> |
</haskell> |
Revision as of 08:40, 2 December 2010
Count the leaves of a binary tree
A leaf is a node with no successors. Write a predicate count_leaves/2 to count them.
countLeaves Empty = 0
countLeaves (Branch a Empty Empty) = 1
countLeaves (Branch a left right) = count_leaves left + count_leaves right