
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find sum of all left leaves in a given Binary Tree in C++
In this problem, we are given a binary tree. Our task is to find the sum of all left leaves in a given Binary Tree.
Let's take an example to understand the problem,
Input :
Output : 11
Explanation −
All leaf nodes of the tree are : 2, 9 Sum = 2 + 9 = 11
Solution Approach
A simple solution to the problem is traversing the tree from root to leaf. If a node is a left leaf node, add it to sum. When the whole tree is traversed. Print Sum.
Example
Program to illustrate the working of our solution
#include <iostream> using namespace std; struct Node{ int key; struct Node* left, *right; }; Node *newNode(char k){ Node *node = new Node; node->key = k; node->right = node->left = NULL; return node; } bool isLeafNode(Node *node){ if (node == NULL) return false; if (node->left == NULL && node->right == NULL) return true; return false; } int findLeftLeavesSum(Node *root){ int sum = 0; if (root != NULL){ if (isLeafNode(root->left)) sum += root->left->key; else sum += findLeftLeavesSum(root->left); sum += findLeftLeavesSum(root->right); } return sum; } int main(){ struct Node *root = newNode(5); root->left = newNode(4); root->right = newNode(6); root->left->left = newNode(2); root->left->right = newNode(1); root->right->left = newNode(9); root->right->right = newNode(7); cout<<"The sum of left leaves of the tree is "<<findLeftLeavesSum(root); return 0; }
Output
The sum of left leaves of the tree is 11
Another approach using Iteration
We will perform depth first search traversal on the tree, And then check if the current node is a left leaf. If Yes, add its value to sum, otherwise, leave. At the end, print the sum.
Example
Program to illustrate the working of our solution
#include<bits/stdc++.h> using namespace std; struct Node{ int key; struct Node* left, *right; }; Node *newNode(char k){ Node *node = new Node; node->key = k; node->right = node->left = NULL; return node; } int findLeftLeavesSum(Node* root){ if(root == NULL) return 0; stack<Node*> treeNodes; treeNodes.push(root); int sum = 0; while(treeNodes.size() > 0){ Node* currentNode = treeNodes.top(); treeNodes.pop(); if (currentNode->left != NULL){ treeNodes.push(currentNode->left); if(currentNode->left->left == NULL && currentNode->left->right == NULL){ sum += currentNode->left->key ; } } if (currentNode->right != NULL) treeNodes.push(currentNode->right); } return sum; } int main(){ Node *root = newNode(5); root->left= newNode(4); root->right = newNode(6); root->left->left = newNode(2); root->left->right = newNode(1); root->right->left = newNode(9); root->right->right= newNode(7); cout<<"The sum of left leaves of the tree is "<<findLeftLeavesSum(root); return 0; }
Output
The sum of left leaves of the tree is 11
Approach 3, using BFS
We will perform a breadth first search with a variable to indicate whether the node is left child or not. If it is, add it to the sum, else leave. At the end, print sum.
Example
Program to illustrate the working of our solution
#include<bits/stdc++.h> using namespace std; struct Node{ int key; struct Node* left, *right; }; Node *newNode(char k){ Node *node = new Node; node->key = k; node->right = node->left = NULL; return node; } int findLeftLeavesSum(Node* root) { if (root == NULL) return 0; queue<pair<Node*, bool> > leftTreeNodes; leftTreeNodes.push({ root, 0 }); int sum = 0; while (!leftTreeNodes.empty()) { Node* temp = leftTreeNodes.front().first; bool is_left_child = leftTreeNodes.front().second; leftTreeNodes.pop(); if (!temp->left && !temp->right && is_left_child) sum = sum + temp->key; if (temp->left) { leftTreeNodes.push({ temp->left, 1 }); } if (temp->right) { leftTreeNodes.push({ temp->right, 0 }); } } return sum; } int main(){ Node *root = newNode(5); root->left= newNode(4); root->right = newNode(6); root->left->left = newNode(2); root->left->right = newNode(1); root->right->left = newNode(9); root->right->right= newNode(7); cout<<"The sum of left leaves of the tree is "<<findLeftLeavesSum(root); return 0; }
Output
The sum of left leaves of the tree is 11
- Related Articles
- Find sum of all right leaves in a given Binary Tree in C++
- Find the sum of left leaf nodes of a given Binary Tree in C++
- Find sum of all nodes of the given perfect binary tree in C++
- Find Leaves of Binary Tree in C++
- Program to find sum of the right leaves of a binary tree in C++
- Print all nodes in a binary tree having K leaves in C++
- Python Program to Find the Sum of All Nodes in a Binary Tree
- Print all k-sum paths in a binary tree in C++
- Program to find sum of all numbers formed by path of a binary tree in python
- Maximum sum of non-leaf nodes among all levels of the given binary tree in C++
- Program to delete all leaves with even values from a binary tree in Python
- Print all leaf nodes of a binary tree from right to left in C++
- Finding Sum of Left Leaves of a BST in JavaScript
- Find mirror of a given node in Binary tree in C++
- Program to find out the largest sum value of a BST in a given binary tree in Python
