

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find the sum of left leaf nodes of a given Binary Tree in C++
Let us suppose we have a Binary Tree having a root node and its left child and right child. The task is to find the total sum of leaf nodes of the tree which are left to its parent node.
For Example
Input-1:
Output:
15
Explanation: In the given input Binary Tree, the sum of all the left leaf nodes is 9+4+2 = 15. So, the output is 15.
Approach to Solve this Problem
We have a Binary Tree and the task is to find the sum of all the leaf nodes which are left to its parent.
The recursive approach to solve this problem is to check if the left node of the root node is empty or not. If it is empty, then calculate the sum for its left node and find the recursive sum for the right node. Thus, for every node, we will check recursively and find its sum.
- Take a Binary Tree having root nodes and its left child as well as right child as the input.
- An integer function leftLeafSum(treenode*root) takes the root node as the input and returns the sum of all the leaf nodes which are left to its parent.
- If the root node is empty or NULL, then return 'zero' otherwise check for the left node of the root node.
- If the left node of the root node is having no children, then recursively check for the right node.
- Return the sum recursively for the left child and the right child.
Example
#include<bits/stdc++.h> using namespace std; struct treenode { int data; treenode * left; treenode * right; }; struct treenode * createNode(int d) { struct treenode * root = new treenode; root -> data = d; root -> left = NULL; root -> right = NULL; return root; } int leftLeafSum(treenode * root) { if (root == NULL) { return 0; } if (root -> left and!root -> left -> left and!root -> left -> right) { return root -> left -> data + leftLeafSum(root -> right); } return leftLeafSum(root -> left) + leftLeafSum(root -> right); } int main() { struct treenode * root = NULL; root = createNode(4); root -> left = createNode(2); root -> right = createNode(2); root -> left -> right = createNode(7); root -> left -> left = createNode(5); root -> right -> left = createNode(5); root -> right -> right = createNode(7); int sum = leftLeafSum(root); cout << sum << endl; return 0; }
Running the above code will generate the output as,
Output
10
Explanation: The leaf nodes in the left nodes are 5 and 5 which are left to their parent, thus the sum of all the leaf node is = 10.
- Related Questions & Answers
- Maximum sum of non-leaf nodes among all levels of the given binary tree in C++
- Find sum of all nodes of the given perfect binary tree in C++
- Print all leaf nodes of a binary tree from right to left in C++
- Product of all leaf nodes of binary tree in C++
- Find sum of all left leaves in a given Binary Tree in C++
- Program to find leaf and non-leaf nodes of a binary tree in Python
- C++ Program to Find Deepest Left Leaf in a Binary Tree
- C++ Pairwise Swap Leaf Nodes in a Binary Tree
- Find height of a special binary tree whose leaf nodes are connected in C++
- Print All Leaf Nodes of a Binary Tree from left to right using Iterative Approach in C++
- Count Non-Leaf nodes in a Binary Tree in C++
- Deepest left leaf node in a binary tree in C++
- Print leaf nodes in binary tree from left to right using one stack in C++
- Python Program to Find the Sum of All Nodes in a Binary Tree
- Find the closest leaf in a Binary Tree in C++