Sum of Left Leaves

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.

Some thoughts on implementation -

Traverse the tree, starting from the root. If a left leaf node is reached, then add its value to the sum. It can be tracked easily that whether the current node is a leaf node or not. But additionally it is required to know that whether the node is a left node or not. It can also be done if we track the node’s alignment right from the beginning.

Here’s an implementation -