Give an algorithm for finding the sum of all elements in a binary tree.

In the above binary tree sum = 106.
The idea is to recursively, call left subtree sum, right subtree sum and add their values to current node’s data.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int key;
Node* left, *right;
};
Node* newNode(int key)
{
Node* node = new Node;
node->key = key;
node->left = node->right = NULL;
return (node);
}
int addBT(Node* root)
{
if (root == NULL)
return 0;
return (root->key + addBT(root->left) + addBT(root->right));
}
int main()
{
Node* root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
root->right->left = newNode(6);
root->right->right = newNode(7);
root->right->left->right = newNode(8);
int sum = addBT(root);
cout << "Sum of all the elements is: " << sum << endl;
return 0;
}
|
Java
class GFG
{
static class Node
{
int key;
Node left, right;
}
static Node newNode(int key)
{
Node node = new Node();
node.key = key;
node.left = node.right = null;
return (node);
}
static int addBT(Node root)
{
if (root == null)
return 0;
return (root.key + addBT(root.left) +
addBT(root.right));
}
public static void main(String args[])
{
Node root = newNode(1);
root.left = newNode(2);
root.right = newNode(3);
root.left.left = newNode(4);
root.left.right = newNode(5);
root.right.left = newNode(6);
root.right.right = newNode(7);
root.right.left.right = newNode(8);
int sum = addBT(root);
System.out.println("Sum of all the elements is: " + sum);
}
}
|
Python3
class newNode:
def __init__(self, key):
self.key = key
self.left = None
self.right = None
def addBT(root):
if (root == None):
return 0
return (root.key + addBT(root.left) +
addBT(root.right))
if __name__ == '__main__':
root = newNode(1)
root.left = newNode(2)
root.right = newNode(3)
root.left.left = newNode(4)
root.left.right = newNode(5)
root.right.left = newNode(6)
root.right.right = newNode(7)
root.right.left.right = newNode(8)
sum = addBT(root)
print("Sum of all the nodes is:", sum)
|
C#
using System;
public class GFG
{
public class Node
{
public int key;
public Node left, right;
}
public static Node newNode(int key)
{
Node node = new Node();
node.key = key;
node.left = node.right = null;
return (node);
}
public static int addBT(Node root)
{
if (root == null)
{
return 0;
}
return (root.key + addBT(root.left) + addBT(root.right));
}
public static void Main(string[] args)
{
Node root = newNode(1);
root.left = newNode(2);
root.right = newNode(3);
root.left.left = newNode(4);
root.left.right = newNode(5);
root.right.left = newNode(6);
root.right.right = newNode(7);
root.right.left.right = newNode(8);
int sum = addBT(root);
Console.WriteLine("Sum of all the elements is: " + sum);
}
}
|
Javascript
<script>
class Node
{
constructor(key)
{
this.key=key;
this.left=this.right=null;
}
}
function addBT(root)
{
if (root == null)
return 0;
return (root.key + addBT(root.left) +
addBT(root.right));
}
let root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(5);
root.right.left = new Node(6);
root.right.right = new Node(7);
root.right.left.right = new Node(8);
let sum = addBT(root);
document.write("Sum of all the elements is: " + sum);
</script>
|
OutputSum of all the elements is: 36
Method 2 – Another way to solve this problem is by using Level Order Traversal. Every time when a Node is deleted from the queue, add it to the sum variable.
Implementation:
C++
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
struct Node {
int key;
struct Node *left, *right;
};
Node* newNode(int key)
{
Node* temp = new Node;
temp->key = key;
temp->left = temp->right = NULL;
return (temp);
}
int sumBT(Node* root)
{
int sum = 0;
queue<Node*> q;
q.push(root);
while (!q.empty()) {
Node* temp = q.front();
q.pop();
sum += temp->key;
if (temp->left) {
q.push(temp->left);
}
if (temp->right) {
q.push(temp->right);
}
}
return sum;
}
int main()
{
Node* root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
root->right->left = newNode(6);
root->right->right = newNode(7);
root->right->left->right = newNode(8);
cout << "Sum of all elements in the binary tree is: "
<< sumBT(root);
}
|
Java
import java.util.LinkedList;
import java.util.Queue;
class GFG {
static class Node {
int key;
Node left, right;
}
static Node newNode(int key)
{
Node node = new Node();
node.key = key;
node.left = node.right = null;
return (node);
}
static int sumBT(Node root)
{
int sum = 0;
Queue<Node> q = new LinkedList<Node>();
q.add(root);
while (!q.isEmpty()) {
Node temp = q.poll();
sum += temp.key;
if (temp.left != null) {
q.add(temp.left);
}
if (temp.right != null) {
q.add(temp.right);
}
}
return sum;
}
public static void main(String args[])
{
Node root = newNode(1);
root.left = newNode(2);
root.right = newNode(3);
root.left.left = newNode(4);
root.left.right = newNode(5);
root.right.left = newNode(6);
root.right.right = newNode(7);
root.right.left.right = newNode(8);
int sum = sumBT(root);
System.out.println(
"Sum of all elements in the binary tree is: "
+ sum);
}
}
|
Python3
class newNode:
def __init__(self, key):
self.key = key
self.left = None
self.right = None
def sumBT(root):
sum = 0
q = []
q.append(root)
while len(q) > 0:
temp = q.pop(0)
sum += temp.key
if (temp.left != None):
q.append(temp.left)
if temp.right != None:
q.append(temp.right)
return sum
if __name__ == '__main__':
root = newNode(1)
root.left = newNode(2)
root.right = newNode(3)
root.left.left = newNode(4)
root.left.right = newNode(5)
root.right.left = newNode(6)
root.right.right = newNode(7)
root.right.left.right = newNode(8)
print("Sum of all elements in the binary tree is: ", sumBT(root))
|
C#
using System;
using System.Collections.Generic;
public class GFG {
public class Node {
public int key;
public Node left, right;
}
public static Node newNode(int key)
{
Node node = new Node();
node.key = key;
node.left = node.right = null;
return (node);
}
public static int sumBT(Node root)
{
int sum = 0;
Queue<Node> q = new Queue<Node>();
q.Enqueue(root);
while (q.Count!=0) {
Node temp = q.Dequeue();
sum += temp.key;
if (temp.left != null) {
q.Enqueue(temp.left);
}
if (temp.right != null) {
q.Enqueue(temp.right);
}
}
return sum;
}
public static void Main(string[] args)
{
Node root = newNode(1);
root.left = newNode(2);
root.right = newNode(3);
root.left.left = newNode(4);
root.left.right = newNode(5);
root.right.left = newNode(6);
root.right.right = newNode(7);
root.right.left.right = newNode(8);
Console.WriteLine("Sum of all elements in the binary tree is: "
+ sumBT(root));
}
}
|
Javascript
<script>
class Node
{
constructor(key)
{
this.key=key;
this.left=this.right=null;
}
}
function sumBT(root)
{
let sum = 0;
let q = [];
q.push(root);
while (q.length != 0) {
let temp = q.shift();
sum += temp.key;
if (temp.left != null) {
q.add(temp.left);
}
if (temp.right != null) {
q.add(temp.right);
}
}
return sum;
}
let root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(5);
root.right.left = new Node(6);
root.right.right = new Node(7);
root.right.left.right = new Node(8);
document.write("Sum of all elements in the binary tree is: " + sumBT(root));
</script>
|
OutputSum of all elements in the binary tree is: 36
Time Complexity: O(n)
Auxiliary Space: O(n)
This article is contributed by Prakriti Gupta. 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.