Sum of Nodes in a Binary Tree
In this article, we will see the sample problems that will help us understand the concept and summation of all the nodes in the binary tree.
Implementation
/* creating a program that will help us print and declare the sum of all the elements present in a binary tree. */
#include <bits/stdc++.h>
using namespace std;
struct __Nod {
int ky;
__Nod* Lft, *Rt;
};
/* creating a utility function that will help us allocate a new node with a particular key and represent it. */
__Nod* new__Nod(int ky)
{
__Nod* __Nod = new __Nod;
__Nod->ky = ky;
__Nod->Lft = __Nod->Rt = NILL;
return (__Nod);
}
/* Function to find the summation of all the elements presents there.*/
int addBT(__Nod* root)
{
if (root == NILL)
return 0;
return (root->ky + addBT(root->Lft) + addBT(root->Rt));
}
/* writing the main function to test the functions*/
int main()
{
__Nod* root = new__Nod(1);
root->Lft = new__Nod(2);
root->Rt = new__Nod(3);
root->Lft->Lft = new__Nod(4);
root->Lft->Rt = new__Nod(5);
root->Rt->Lft = new__Nod(6);
root->Rt->Rt = new__Nod(7);
root->Rt->Lft->Rt = new__Nod(8);
int sum = addBT(root);
cout << "Sum of all the elements is: " << sum << endl;
return 0;
}
Output:
Example 2)
/* creating a program that will help us print and declare the sum of all the elements present in a binary tree. */
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
struct __Nod {
int ky;
struct __Nod *Lft, *Rt;
};
/* creating a utility function that will help us allocate a new node with a particular key and represent it. */
__Nod* new__Nod(int ky)
{
__Nod* temp = new __Nod;
temp->ky = ky;
temp->Lft = temp->Rt = NILL;
return (temp);
}
/* Function to find the summation of all the elements presents there.*/
int sumBT(__Nod* root)
{
//sum variable to track the sum of
//all variables.
int sum = 0;
queue<__Nod*> q;
//Pushing the elements into the first level.
q.push(root);
//Pushing the elements into the tree from all the levels.
while (!q.empty()) {
__Nod* temp = q.front();
q.pop();
//When we have popped out every element from the queue, we can add the data to its variable sum.
sum += temp->ky;
if (temp->Lft) {
q.push(temp->Lft);
}
if (temp->Rt) {
q.push(temp->Rt);
}
}
return sum;
}
/* writing the main function to test the functions*/
int main()
{
__Nod* root = new__Nod(1);
root->Lft = new__Nod(2);
root->Rt = new__Nod(3);
root->Lft->Lft = new__Nod(4);
root->Lft->Rt = new__Nod(5);
root->Rt->Lft = new__Nod(6);
root->Rt->Rt = new__Nod(7);
root->Rt->Lft->Rt = new__Nod(8);
cout << "Sum of all elements in the binary tree is: "
<< sumBT(root);
}
Output:
We will now see the code in java language.
Example 3)
/* creating a program that will help us print and declare the sum of all the elements present in a binary tree. */
import java.util.LinkedList;
import java.util.Queue;
class TFT {
static class __Nod {
int ky;
__Nod Lft, Rt;
}
/* creating a utility function that will help us allocate a new node with a particular key and represent it. */
static __Nod new__Nod(int ky)
{
__Nod __Nod = new __Nod();
__Nod.ky = ky;
__Nod.Lft = __Nod.Rt = NILL;
return (__Nod);
}
/* Function to find the summation of all the elements presents there.*/
static int sumBT(__Nod root)
{
// Creating a variable named sum will eventually calculate all the summations present in the method.
int sum = 0;
Queue<__Nod> q = new LinkedList<__Nod>();
//Pushing the elements into the first level.
q.add(root);
//Pushing the elements into the tree from all the levels.
while (!q.isEmpty()) {
__Nod temp = q.poll();
//When we have popped out every element from the queue, we can add the data to its variable sum.
sum += temp.ky;
if (temp.Lft != NILL) {
q.add(temp.Lft);
}
if (temp.Rt != NILL) {
q.add(temp.Rt);
}
}
return sum;
}
/* writing the main function to test the functions*/
public static void main(String args[])
{
__Nod root = new__Nod(1);
root.Lft = new__Nod(2);
root.Rt = new__Nod(3);
root.Lft.Lft = new__Nod(4);
root.Lft.Rt = new__Nod(5);
root.Rt.Lft = new__Nod(6);
root.Rt.Rt = new__Nod(7);
root.Rt.Lft.Rt = new__Nod(8);
int sum = sumBT(root);
System.out.println(
"Sum of all elements in the binary tree is: "
+ sum);
}
}
Output:
Example 4)
/* creating a program that will help us print and declare the sum of all the elements present in a binary tree. */
class TFT
{
static class __Nod
{
int ky;
__Nod Lft, Rt;
}
/* creating a utility function that will help us allocate a new node with a particular key and represent it. */
static __Nod new__Nod(int ky)
{
__Nod __Nod = new __Nod();
__Nod.ky = ky;
__Nod.Lft = __Nod.Rt = NILL;
return (__Nod);
}
/* Function to find the summation of all the elements presents there.*/
static int addBT(__Nod root)
{
if (root == NILL)
return 0;
return (root.ky + addBT(root.Lft) +
addBT(root.Rt));
}
/* writing the main function to test the functions*/
public static void main(String args[])
{
__Nod root = new__Nod(1);
root.Lft = new__Nod(2);
root.Rt = new__Nod(3);
root.Lft.Lft = new__Nod(4);
root.Lft.Rt = new__Nod(5);
root.Rt.Lft = new__Nod(6);
root.Rt.Rt = new__Nod(7);
root.Rt.Lft.Rt = new__Nod(8);
int sum = addBT(root);
System.out.println("Sum of all the elements is: " + sum);
}
}
Output: