Sum of all leaf nodes of binary tree
Last Updated :
18 Jan, 2023
Given a binary tree, find the sum of all the leaf nodes.
Examples:
Input :
1
/ \
2 3
/ \ / \
4 5 6 7
\
8
Output :
Sum = 4 + 5 + 8 + 7 = 24
The idea is to traverse the tree in any fashion and check if the node is the leaf node or not. If the node is the leaf node, add node data to sum variable.
Following is the implementation of above approach.
C++
// CPP program to find sum of
// all leaf nodes of binary tree
#include<bits/stdc++.h>
using namespace std;
// struct binary tree node
struct Node{
int data;
Node *left, *right;
};
// return new node
Node *newNode(int data){
Node *temp = new Node();
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
// utility function which calculates
// sum of all leaf nodes
void leafSum(Node *root, int& sum){
if (!root)
return;
// add root data to sum if
// root is a leaf node
if (!root->left && !root->right)
sum += root->data;
// propagate recursively in left
// and right subtree
leafSum(root->left, sum);
leafSum(root->right, sum);
}
// driver program
int main(){
//construct binary tree
Node *root = newNode(1);
root->left = newNode(2);
root->left->left = newNode(4);
root->left->right = newNode(5);
root->right = newNode(3);
root->right->right = newNode(7);
root->right->left = newNode(6);
root->right->left->right = newNode(8);
// variable to store sum of leaf nodes
int sum = 0;
leafSum(root, sum);
cout << sum << endl;
return 0;
}
Java
// Java program to find sum of
// all leaf nodes of binary tree
public class GFG {
// user define class node
static class Node{
int data;
Node left, right;
// constructor
Node(int data){
this.data = data;
left = null;
right = null;
}
}
static int sum;
// utility function which calculates
// sum of all leaf nodes
static void leafSum(Node root){
if (root == null)
return;
// add root data to sum if
// root is a leaf node
if (root.left == null && root.right == null)
sum += root.data;
// propagate recursively in left
// and right subtree
leafSum(root.left);
leafSum(root.right);
}
// driver program
public static void main(String args[])
{
//construct binary tree
Node root = new Node(1);
root.left = new Node(2);
root.left.left = new Node(4);
root.left.right = new Node(5);
root.right = new Node(3);
root.right.right = new Node(7);
root.right.left = new Node(6);
root.right.left.right = new Node(8);
// variable to store sum of leaf nodes
sum = 0;
leafSum(root);
System.out.println(sum);
}
}
// This code is contributed by Sumit Ghosh
Python3
# Python3 Program to find the
#sum of leaf nodes of a binary tree
# Class for node creation
class Node:
# Constructor
def __init__(self, data):
self.data = data
self.left = None
self.right = None
# Utility function to calculate
# the sum of all leaf nodes
def leafSum(root):
global total
if root is None:
return
if (root.left is None and root.right is None):
total += root.data
leafSum(root.left)
leafSum(root.right)
# Binary tree Formation
if __name__=='__main__':
root = Node(1)
root.left = Node(2)
root.left.left = Node(4)
root.left.right = Node(5)
root.right = Node(3)
root.right.right = Node(7)
root.right.left = Node(6)
root.right.left.right = Node(8)
# Variable to store the sum of leaf nodes
total = 0
leafSum(root)
# Printing the calculated sum
print(total)
# This code is contributed by Naren Sai Krishna
C#
using System;
// C# program to find sum of
// all leaf nodes of binary tree
public class GFG
{
// user define class node
public class Node
{
public int data;
public Node left, right;
// constructor
public Node(int data)
{
this.data = data;
left = null;
right = null;
}
}
public static int sum;
// utility function which calculates
// sum of all leaf nodes
public static void leafSum(Node root)
{
if (root == null)
{
return;
}
// add root data to sum if
// root is a leaf node
if (root.left == null && root.right == null)
{
sum += root.data;
}
// propagate recursively in left
// and right subtree
leafSum(root.left);
leafSum(root.right);
}
// driver program
public static void Main(string[] args)
{
//construct binary tree
Node root = new Node(1);
root.left = new Node(2);
root.left.left = new Node(4);
root.left.right = new Node(5);
root.right = new Node(3);
root.right.right = new Node(7);
root.right.left = new Node(6);
root.right.left.right = new Node(8);
// variable to store sum of leaf nodes
sum = 0;
leafSum(root);
Console.WriteLine(sum);
}
}
// This code is contributed by Shrikant13
JavaScript
<script>
// Javascript program to find sum of
// all leaf nodes of binary tree
// user define class node
class Node
{
constructor(data) {
this.left = null;
this.right = null;
this.data = data;
}
}
let sum;
// utility function which calculates
// sum of all leaf nodes
function leafSum(root){
if (root == null)
return;
// add root data to sum if
// root is a leaf node
if (root.left == null && root.right == null)
sum += root.data;
// propagate recursively in left
// and right subtree
leafSum(root.left);
leafSum(root.right);
}
// construct binary tree
let root = new Node(1);
root.left = new Node(2);
root.left.left = new Node(4);
root.left.right = new Node(5);
root.right = new Node(3);
root.right.right = new Node(7);
root.right.left = new Node(6);
root.right.left.right = new Node(8);
// variable to store sum of leaf nodes
sum = 0;
leafSum(root);
document.write(sum);
// This code is contributed by divyesh072019.
</script>
Time Complexity: O(N), where N is the number of nodes.
Auxiliary Space: O(N), for recursion call stack.
Similar Reads
Sum of all nodes in a binary tree Give an algorithm for finding the sum of all elements in a binary tree. In the above binary tree sum = 106. Recommended PracticeSum of Binary TreeTry It!The idea is to recursively, call left subtree sum, right subtree sum and add their values to current node's data. Implementation: C++ /* Program to
15+ min read
Sum of all the Boundary Nodes of a Binary Tree Given a binary tree, the task is to print the sum of all the boundary nodes of the tree. Examples: Input: 1 / \ 2 3 / \ / \ 4 5 6 7 Output: 28 Input: 1 / \ 2 3 \ / 4 5 \ 6 / \ 7 8 Output: 36 Approach: We have already discussed the Boundary Traversal of a Binary tree. Here we will find the sum of the
10 min read
Sum of all elements of N-ary Tree Given an n-ary tree consisting of n nodes, the task is to find the sum of all the elements in the given n-ary tree.Example:Input:Output: 268Explanation: The sum of all the nodes is 11 + 21 + 29 + 90 + 18 + 10 + 12 + 77 = 268Input:Output: 360Explanation: The sum of all the nodes is 81 + 26 + 23 + 49
5 min read
Sum of nodes at maximum depth of a Binary Tree Given a root node to a tree, find the sum of all the leaf nodes which are at maximum depth from root node. Example: 1 / \ 2 3 / \ / \ 4 5 6 7 Input : root(of above tree) Output : 22 Explanation: Nodes at maximum depth are: 4, 5, 6, 7. So, sum of these nodes = 22 While traversing the nodes compare th
15+ min read
Sum of all vertical levels of a Binary Tree Given a binary tree consisting of either 1 or 0 as its node values, the task is to find the sum of all vertical levels of the Binary Tree, considering each value to be a binary representation. Examples: Input: 1 / \ 1 0 / \ / \ 1 0 1 0Output: 7Explanation: Taking vertical levels from left to right:F
10 min read