Sum of Left Leaves

Posted on

screen-shot-2016-11-15-at-4-25-23-pm

Problem:

Find the sum of all left leaves in a given binary tree.

Example:

3
/ \
9 20
/ \
15 7

There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24.

Solution:

For tree traversal, think in recursion way first. The point is to catch the left leaves. So the base case  includes catching a left leaf. Then just recursively call itself on both left subtree and right subtree to find all the left leaves.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s