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++
#include<bits/stdc++.h>
using namespace std;
struct Node{
int data;
Node *left, *right;
};
Node *newNode( int data){
Node *temp = new Node();
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
void leafSum(Node *root, int & sum){
if (!root)
return ;
if (!root->left && !root->right)
sum += root->data;
leafSum(root->left, sum);
leafSum(root->right, sum);
}
int main(){
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);
int sum = 0;
leafSum(root, sum);
cout << sum << endl;
return 0;
}
|
Java
public class GFG {
static class Node{
int data;
Node left, right;
Node( int data){
this .data = data;
left = null ;
right = null ;
}
}
static int sum;
static void leafSum(Node root){
if (root == null )
return ;
if (root.left == null && root.right == null )
sum += root.data;
leafSum(root.left);
leafSum(root.right);
}
public static void main(String args[])
{
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 );
sum = 0 ;
leafSum(root);
System.out.println(sum);
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self .left = None
self .right = None
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)
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 )
total = 0
leafSum(root)
print (total)
|
C#
using System;
public class GFG
{
public class Node
{
public int data;
public Node left, right;
public Node( int data)
{
this .data = data;
left = null ;
right = null ;
}
}
public static int sum;
public static void leafSum(Node root)
{
if (root == null )
{
return ;
}
if (root.left == null && root.right == null )
{
sum += root.data;
}
leafSum(root.left);
leafSum(root.right);
}
public static void Main( string [] args)
{
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);
sum = 0;
leafSum(root);
Console.WriteLine(sum);
}
}
|
Javascript
<script>
class Node
{
constructor(data) {
this .left = null ;
this .right = null ;
this .data = data;
}
}
let sum;
function leafSum(root){
if (root == null )
return ;
if (root.left == null && root.right == null )
sum += root.data;
leafSum(root.left);
leafSum(root.right);
}
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);
sum = 0;
leafSum(root);
document.write(sum);
</script>
|
Time Complexity: O(N), where N is the number of nodes.
Auxiliary Space: O(N), for recursion call stack.
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!