Counting Leaves Recursively

So help me out here. I was given the following code problem:

and asked to implement a ‘countLeaves’ function that traverses the tree and counts all the leaves. Some examples of trees are below:

I wrote the code rather quickly and surprised myself by writing the following. It’s the first time I’ve felt like I actually *got* recursive functions!

So is the answer really this easy? Let me know what you think!

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">