August 26, 2024
P61 - Count the leaves of a binary tree.
A leaf is a node with no successors. Write a methodleafCountto count them.
> p61:leafCount(#node{value = 23}).
2
erlang
%Count the leaves of a binary tree
-module(p61).
-include("node.hrl").
-export([leafCount/1]).
leafCount(#node{leftNode = L, rightNode = R}) ->
case [L, R] of
[null, null] -> 2;
[null, _] -> 1 ;
[_, null] -> 1 ;
[_, _] -> 0
end.