Sum of Binary Tree | Practice | GeeksforGeeks

Given a Binary Tree of size N, your task is to complete the function sumBt(), that should return the sum of all the nodes of the given binary tree.

Input:
First line of input contains the number of test cases T. For each test case, there will be two lines:


First line of each test case will be an integer N denoting the number of parent child relationships.


Second line of each test case will print the level order traversal of the tree in the form of N space separated triplets. The description of triplets is as follows:


Each triplet will contain three space-separated elements of the form (int, int char).


The first integer element will be the value of parent. 


The second integer will be the value of corresponding left or right child. In case the child is null, this value will be -1.


The third element of triplet which is a character can take any of the three values ‘L’, ‘R’ or ‘N’. L denotes that the children is a left child, R denotes that the children is a Right Child and N denotes that the child is NULL.
		 





First line of each test case will be an integer N denoting the number of parent child relationships.

First line of each test case will be an integer N denoting the number of parent child relationships.

Second line of each test case will print the level order traversal of the tree in the form of N space separated triplets. The description of triplets is as follows:


Each triplet will contain three space-separated elements of the form (int, int char).


The first integer element will be the value of parent. 


The second integer will be the value of corresponding left or right child. In case the child is null, this value will be -1.


The third element of triplet which is a character can take any of the three values ‘L’, ‘R’ or ‘N’. L denotes that the children is a left child, R denotes that the children is a Right Child and N denotes that the child is NULL.
		 



Second line of each test case will print the level order traversal of the tree in the form of N space separated triplets. The description of triplets is as follows:


Each triplet will contain three space-separated elements of the form (int, int char).


The first integer element will be the value of parent. 


The second integer will be the value of corresponding left or right child. In case the child is null, this value will be -1.


The third element of triplet which is a character can take any of the three values ‘L’, ‘R’ or ‘N’. L denotes that the children is a left child, R denotes that the children is a Right Child and N denotes that the child is NULL.
		 



Each triplet will contain three space-separated elements of the form (int, int char).

Each triplet will contain three space-separated elements of the form (int, int char).

The first integer element will be the value of parent. 

The first integer element will be the value of parent. 

The second integer will be the value of corresponding left or right child. In case the child is null, this value will be -1.

The second integer will be the value of corresponding left or right child. In case the child is null, this value will be -1.

The third element of triplet which is a character can take any of the three values ‘L’, ‘R’ or ‘N’. L denotes that the children is a left child, R denotes that the children is a Right Child and N denotes that the child is NULL.
		 

The third element of triplet which is a character can take any of the three values ‘L’, ‘R’ or ‘N’. L denotes that the children is a left child, R denotes that the children is a Right Child and N denotes that the child is NULL.
		 
Please note that the relationships are printed only for internal nodes and not for leaf nodes.

Output:
The function should return the sum of all the nodes of the binary tree.

User Task:
As it is a functional problem. So, you don't need to worry about the input you just have to complete the function sumBT() that takes a node as a parameter and returns the sum of all the nodes.

Constraints:
1<=T<=100
1<=N<=100

Example:
Input:
2
2
1 2 L 1 -1 N
6
1 2 L 1 3 R 2 -1 N 2 -1 N 3 3 L 3 -1 N
Output:
3
9
Note:The Input/Ouput format and Example given are used for system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.
0
superrhitik4582 days ago
//simple recursion based solution..
 
long int sum=0;void add(Node*root){   if(root==NULL)   return ;   sum+=root->key;   add(root->left);   add(root->right);}long int sumBT(Node* root){   // Code hereadd(root);long int s=sum;sum=0;return s;   }
0
harshscode1 week ago
BEST SOLUTION C++
 if(root==NULL)   return 0;      if(root->left==NULL and root->right==NULL)   return root->key;      int l=sumBT(root->left);   int r=sumBT(root->right);      return root->key+l+r;
0
atif836141 month ago
RECURSION BASED PROBLEM :
class BinaryTree
{
    static int sumBT(Node head){
         if(head==null){
             return 0;
         }
         int ls=sumBT(head.left);
         int rs =sumBT(head.right);
         int sum = ls +rs+head.data;
         return sum;
         }
         }
0
0niharika21 month ago
Iterative C++
if(root==NULL)       return -1;   long int sum=0;   stack<Node*> s;   s.push(root);   while(!s.empty())   {       root = s.top();       sum += root->key;       s.pop();       if(root->right!=NULL)           s.push(root->right);       if(root->left!=NULL)           s.push(root->left);   }   return sum;
0
harendersingh78962 months ago
class BinaryTree{   static int sumBT(Node head){       //Code       if(head == null)       {           return 0;       }              int ls = sumBT(head.left);       int rs = sumBT(head.right);              int ts = ls + rs + head.data;              return ts;   }}
0
anjalirajwar1013 months ago
long int sumBT(Node* root){   // Code here   if(root==NULL){       return 0;   }   long int sum;   sum= root->key + sumBT(root->left) +sumBT(root->right);   return sum;}
0
ishnoorsinghsethi3 months ago
C++ solution using level order traversal
long int sumBT(Node* root){   queue<Node *> q;   long int sum = 0;   q.push(root);   while(q.size()){       Node * p =q.front();       q.pop();       sum+=p->key;       if(p->left) q.push(p->left);       if(p->right) q.push(p->right);   }   return sum;   // Code here}
+1
kronizerdeltac3 months ago
C++ AND JAVA SOLUTIONS
 
JAVA
static int sumBT(Node root){
        return root == null ? 0 : sumBT(root.left) + sumBT(root.right) + root.data;
    }
C++
long int sumBT(Node* root)
{
   return root == nullptr ? 0 : sumBT(root->left) + sumBT(root->right) + root->key;
}
0
amiransarimy3 months ago
Python Solution
Total Time Taken:0.2/1.3
 
def sumBT(root):
   if root is None:
       return 0
   return root.data + sumBT(root.left) + sumBT(root.right)
0
punithrkashi3 months ago
Javascript Solution
 
class Solution
{ 
  sumBT(root){
      var sum = 0;
     if(root===null) return (sum + 0);
     if(root.left === null && root.right === null){
         return (sum + root.data)
     }else{
         sum = root.data + sum;
         return (sum + this.sumBT(root.left) + this.sumBT(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.