Sum of leaf nodes in BST | Practice | GeeksforGeeks

Given a Binary Search Tree, find the sum of all leaf nodes. BST has the following property (duplicate nodes are possible):
The left subtree of a node contains only nodes with keys less than the node’s key.
The right subtree of a node contains only nodes with keys greater than or equal to the node’s key.
Input:
The first line of input contains a single integer T denoting the number of test cases. Then T test cases follow. Each test case consists of two lines. The first line of each test case consists of integer N, denoting the number of elements in the BST. The second line of each test case consists of N space-separated integers denoting the elements in the BST.
Output:
For each testcase, in a new line, print the sum of leaf nodes.
Your Task:
This is a function problem. You don't have to take any input. You are required to complete the function sumOfLeafNodes that takes root as parameter and returns the sum of leaf nodes. 
Constraints:
1 <= T <= 103     
1 <= N <= 105
Example:
Input:
2
6
67 34 82 12 45 78
1
45
Output:
135
45
Explanation:
In first test case, the BST for the given input will be-
               67
          /             \
   34                  82
  /    \                /
12   45         78
Hence, the required sum= 12 + 45 + 78 = 135
0
gladsonbernardd2 months ago
def sumOfLeafNodes(root):   #Your code here   if root is None:       return 0   elif root.left is None and root.right is None:       return root.data   return sumOfLeafNodes(root.left)+sumOfLeafNodes(root.right)
0
pranavmani8903 months ago
int randi(Node *root ){    /*Your code here */    if(root==NULL){        return 0;    }    else if(root->left==NULL && root->right==NULL){        return root->data;    }     return randi(root->left)+randi(root->right);}
0
mishrat4763 months ago
 public static int sumOfLeafNodes(Node root)
    {
        // code here 
        if (root == null)
        {
            return 0;
        }
        
        if (root.left == null && root.right == null)
        {
            return root.data;
        }
        int lh = sumOfLeafNodes(root.left);
        int rh = sumOfLeafNodes(root.right);
        
        return lh + rh;
    }
0
aroopghosh5534 months ago
int sum = 0;

void leaf(Node* r){
    if(r == NULL){
        return;
    }
    if(r->left == NULL && r->right == NULL){
        sum = sum + r->data;
    }
    leaf(r->left);
    leaf(r->right);
}

int sumOfLeafNodes(Node *root){
    Node* r = root;
    leaf(r);
    int s = sum;
    sum = 0;
    return s;
}
+2
golaravi5555 months ago
void fuck(Node * root,int &sum){   if(!root){       return;   }     if(!root->left && !root->right){       sum+=root->data;          }   fuck(root->left,sum);   fuck(root->right,sum);}int sumOfLeafNodes(Node *r ){    /*Your code here */    int sum=0;    if(!r){        return 0;    }        fuck(r,sum);    return sum;}
0
purubhargava0115 months ago
int sumOfLeafNodes(Node *root){
    if(root == NULL){
        return 0 ; 
    }
    if(root -> left == NULL  and root -> right == NULL){
        return root -> data ; 
    }
    return sumOfLeafNodes( root -> left ) + sumOfLeafNodes( root -> right ) ; 
}
+1
badgujarsachin836 months ago
int sumOfLeafNodes(Node *r ){
     /*Your code here */
     if(r==NULL){
         return 0;
     }
     if(r->left==NULL && r->right==NULL){
         return r->data;
     }
     return sumOfLeafNodes(r->left)+sumOfLeafNodes(r->right);
}
-1
user_8m8m6 months ago
can you help me guys , where i am going wrong
int sum = 0;
int sumOfLeafNodes(Node *r ){    /*Your code here */        if(r!=NULL)    {        sum = sumOfLeafNodes(r->left);        if(r->left == NULL && r->right == NULL)        {           printf("%d ",r->data);           sum += r->data;          }        sum = sumOfLeafNodes(r->right);    }      return(sum); }
0
swrangpilton7 months ago
C++
int sumOfLeafNodes(Node *root){   if(!root) return 0;   else if(!root->left && !root->right){       return root->data;   } else {       return sumOfLeafNodes(root->left) + sumOfLeafNodes(root->right);   }}
0
Dhruv Mishra_18267 months ago
Dhruv Mishra_1826
exe time 0.01
void traverse(Node* root, int &sum){    if (root==NULL){        return;    }    traverse(root->left,sum);    if (root->left==NULL && root->right==NULL){        sum=sum+(root->data);    }    traverse(root->right,sum);}
int sumOfLeafNodes(Node *r ){     /*Your code here */     int sum=0;     traverse(r,sum);     return sum;}
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.