Sum of Leaf Nodes | Practice | GeeksforGeeks

Given a Binary Tree of size N. The task is to complete the function sumLeaf(), that should return the sum of all the leaf nodes of the given binary tree.
Input:
First line of input contains number of testcases T. For each testcase, there will be two lines, first of which containing the number of edges (between two nodes) in the tree. Next line contains N pairs (considering a and b) with a 'L' (means node b on left of a) or 'R' (means node b on right of a) after a and b.
Output:
For each testcase, there will be a single line containing the sum of all leaf nodes in the tree.
User Task: 
The task is to complete the function sumLeaf() which takes root reference as argument and returns the sum of all leaf nodes.
Constraints:
1 <= T <= 100
1 <= N <= 103
Example:
Input:
2
2
1 2 L 1 3 R
5
10 20 L 10 30 R 20 40 L 20 60 R 30 90 L
Output:
5
190
Explanation:
Testcase 1: Leaf nodes in the tree are 2 and 3, and their sum is 5.
 
0
harshscode1 week ago
EASY AND EFFICIENT SOLUTION......
       if(root==NULL)      return 0;            if(root->left==NULL and root->right==NULL)      return root->data;            int sum1=sumLeaf(root->left);      int sum2=sumLeaf(root->right);            return sum1+sum2;
0
hanumanmanyam8373 weeks ago
class GfG
{
    int sum=0;
    public int SumofLeafNodes(Node root)
    {
        // your code here
        if(root==null)
        {
            return 0;
        }
        if(root.left==null && root.right==null)
        {
            sum+= root.data;
        }
        SumofLeafNodes(root.left);
        SumofLeafNodes(root.right);
        return sum;
        
        
    }
}
0
roopsaisurampudi1 month ago
if (root == null) return 0;
        if (root.left == null && root.right == null) return root.data;
        return SumofLeafNodes(root.left) + SumofLeafNodes(root.right);
0
mohitm15092 months ago
class GfG{   public int SumofLeafNodes(Node root)   {       // your code here       if(root == null) return 0;       int sum = 0;       if(root.left == null && root.right == null)           sum += root.data;       sum += SumofLeafNodes(root.left);       sum += SumofLeafNodes(root.right);              return sum;   }}
 
0
sumandas203 months ago
int sumLeaf(Node* root){   // Code here   if(root==NULL)return 0;   if(root->left==NULL&&root->right==NULL){       return root->data;   }   return sumLeaf(root->left)+sumLeaf(root->right);}
0
sengarharsh9293 months ago
class GfG{   int sum=0;   public int SumofLeafNodes(Node root)   {       // your code here       iif(root==null )return 0;       if(root.left==null && root.right==null)       {           sum=sum+root.data;       }       if(root.left!=null)       {           SumofLeafNodes(root.left);       }       if(root.right!=null)       {           SumofLeafNodes(root.right);       }       return sum;          }}
+1
shreyash9779663 months ago
int sumLeaf(Node* root)
{
    // Code here
    if(root==NULL) return 0;
    if(root->left==NULL and root->right==NULL){
        return root->data;
    }
    return (sumLeaf(root->left)+sumLeaf(root->right));
}
+1
sarthakkashyap1903 months ago
C++ soln.
void col(Node* root, int &sum){
    if(!root){
        return;
    }
    if(root->left==NULL && root->right==NULL){
        sum+=root->data;
        return;
    }
    col(root->left, sum);
    col(root->right, sum);
    return;
}
int sumLeaf(Node* root)
{
    // Code here
    int sum = 0;
    col(root, sum);
    return sum;
    
}
+1
maskiyashank883 months ago
Simple Java Code 0.4 time
public int SumofLeafNodes(Node node)
    {
        // your code here
        if(node==null)
            return 0;
        if(node.left == null && node.right == null)
            return node.data;
        return SumofLeafNodes(node.left) + SumofLeafNodes(node.right);
    }
-1
dhirunand3 months ago
class GfG
{
    public int SumofLeafNodes(Node root)
    {
        // your code here
        count = 0;
        solve(root);
        
        return count;
    }
    
    static int count;
    public static void solve(Node root){

        if(root.left == null && root.right == null){
            count+=root.data;
            return;
        }
        
        if(root.left==null && root.right != null)
            solve(root.right);
        
        if(root.right==null && root.left != null)
            solve(root.left);
            
        if(root.right!=null && root.left != null){
            solve(root.left);
            solve(root.right);
        }
    }
}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.