Write a program to find the sum of all left leaves of a given binary tree. For example, for the following shown tree output of the program should be 15 as there are two left leaves - node 9 and node 6.
Video coming soon!
Subscribe for more updates
Preparing for interviews? IDeserve team is here to help.
Create your profile
Create your profile, and here is what you will get:
1: Interview practice platform.
2: Once you are ready to take the interview, IDeserve team will help you get connected to the best job opportunities.
3: Personalized mentorship from IDeserve team once your interview process has started.
Creation of profile shouldn't take more than 2 minutes.
The solution to this problem is as simple as traversing the complete tree using any traversal method in order to visit all nodes and checking if a node has a left-child which is also a leaf node. If that is the case then add that left-child's value to the sum. As you can see in code snippet, we have used pre-order traversal to implement this algorithm.
Please checkout findLeftLeavesSum(TreeNode currentNode, int leftLeavesSum) function in code snippet for implementation details.
Support us by whitelisting IDeserve in your ad-blocker.